Search code examples
tapestry

Any tutorial for tapestry-mongodb module?


I'm using Tapestry 5.4beta6 now and I can see there is tapestry-mongodb module. Is there any example how can I use this in an application? I found the former module on github but there is only a note that module is part of core framework but no docs or example ...


Solution

  • tapestry-mongodb is just wrapping the initialization of the MongoDB Java driver. You're free to use whatever POJO-wrapper you like; there are plenty of them:

    • Morphia - Type-Safe Wrapper with DAO/Datastore abstractions
    • Mungbean (w/clojure support)
    • Spring MongoDB – Provides Spring users with a familiar data access features including rich POJO mapping.
    • DataNucleus JPA/JDO JPA/JDO wrapper
    • lib-mongomapper
    • JavaBean Mapper (No annotations)
    • MongoJack – Uses jackson (annotations) to map to/from POJOs and has a simple wrapper around DBCollection to simply this.
    • Kundera
    • JPA compliant ORM. Works with multiple datastores.
    • Jongo – Query in Java as in mongo shell (using strings), unmarshall results into Java objects (using Jackson)
    • MongoLink – Object Document Mapper (ODM.) Uses a plain java DSL for mapping declaration. (Source: docs.mongodb.org)

    I took this example from the tapestry-mongodb tests, which uses Jongo as ORM:

    Module

    public class MongoDBTestModule
    {
    
        public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration)
        {
            configuration.add(MongoDBSymbols.DEFAULT_DB_NAME, "TapestryMongoTest");
        }
    
    
        public static void contributeMongoDBSource(OrderedConfiguration<ServerAddress> configuration)
        {
            try
            {
                configuration.add("test", new ServerAddress("localhost", 12345));
            }
            catch (UnknownHostException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    

    Person entity:

    package org.apache.tapestry5.internal.mongodb;
    
    import org.bson.types.ObjectId;
    
    import java.util.Date;
    
    /**
     *
     */
    public class People
    {
        private ObjectId _id;
    
        private String name;
        private String surname;
        private Date birthDate;
    
    
        public ObjectId get_id() {
            return _id;
        }
    
        public void set_id(ObjectId _id) {
            this._id = _id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSurname() {
            return surname;
        }
    
        public void setSurname(String surname) {
            this.surname = surname;
        }
    
        public Date getBirthDate() {
            return birthDate;
        }
    
        public void setBirthDate(Date birthDate) {
            this.birthDate = birthDate;
        }
    }
    

    Actual test code

    @Inject
    MongoDB mongoDB;
    
    void test() { 
        // ...
        org.jongo.Jongo jongo = new Jongo(mongoDB.getDefaultMongoDb());
        org.jongo.MongoCollection peoples = jongo.getCollection("peoples");
        // ...
    }