Advanced Usage

1.   Once we've annotated our objects, all we need to do is create an instance of Morphia (It is recommended that you create this instance once, and reuse it.), tell it which classes we want to map, and then we can start mapping between Mongo documents and Java objects:

Morphia morphia = new Morphia();
morphia.map(BlogEntry.class).map(Author.class);
 

Each class that you map will be validated, and a MappingException will be thrown if the class is not valid for some reason. You can also tell Morphia to scan a package, and map all classes found in that package:

morphia.mapPackage("my.package.with.only.mongo.entities"); 
 

 

2.   It is possible to manually use the Morphia instance to map to and from DBObjects to interact with the java driver directly. We can just call the toDBObject() method on our Morphia instance, passing the Java object and then save the resulting DBObject directly to Mongo:

DB db = mongo.getDB("BlogSite");
BlogEntry blogEntry = ...; // this is our annotated object

// map the blog entry to a Mongo DBObject
DBObject blogEntryDbObj = morphia.toDBObject(blogEntry);
// and then save that DBObject in a Mongo collection
db.getCollection("BlogEntries").save(blogEntryDbObj); 
 

We can also call the fromDBObject() method on our Morphia instance, passing in the DBObject retrieved from Mongo:

DB db = mongo.getDB("BlogSite");
String blogEntryId = ...; // the ID of the blog entry we want to load

// load the DBObject from a Mongo collection
BasicDBObject blogEntryDbObj = (BasicDBObject) db.getCollection("BlogEntries").findOne(new BasicDBObject("_id", new ObjectId(blogEntryId));
// and then map it to our BlogEntry object
BlogEntry blogEntry = morphia.fromDBObject(BlogEntry.class, blogEntryDbObj);
 

 

3.   It is considered good practice to abstract the underlying persistence strategy away from the calling code, by encapsulating the persistence calls within Data Access Objects (DAOs). Morphia supports this style by providing an abstract BasicDAO implementation, based on the DAO interface, that uses the Datastore to persist, and query for java POJOs. This abstract class implements all the basic DAO methods you would want to use to create/update, read, and delete objects.

4.   by having your DAO class extend the BasicDAO class, you would normally only need to implement finder methods to return query results for you domain objects:

public class BlogEntryDAO extends BasicDAO<BlogEntry, ObjectId> {
    public BlogEntryDAO( Morphia morphia, Mongo mongo ) {
        super(mongo, morphia, "myBlogDb");
    }
}
 

Since all the methods are implemented for us, we only need to do two important thing:

 

    a)   Implement a constructor. The constructor passes information on to the DAO superclass.

    b)   Implement finder methods

5.   The Mongo driver keeps a connection pool per Mongo instance. If you wish to release those resources, just make sure you stop using the Mongo instance.

6.   A Reference is made up of the collection name + the _id field value. In capped collections the _id field is not unique and references to capped collection might be ambiguous.

猜你喜欢

转载自seanzhou.iteye.com/blog/1669448