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.
分享到:
相关推荐
This document covers intermediate and advanced usage of the RArray classes. It gives explanations and examples for some of the most frequently asked questions and encountered problems that have ...
* Add a field for advanced usage on tagcloud widget * Version 2.0-beta6 : * Add Japanese translation (thanks - kazuhisa) * Fix a bug with search and taxonomy param for mass edit terms. (ticket #233...
The book's tutorial style, step-by-step instruction will teach you everything you need to know to use the SAP Query tools, including its configuration, advanced usage, and integration to Microsoft....
This book walks you through examples that progressively build upon your knowledge, taking you from beginner to advanced usage in a series of easy-to-follow steps. We begin with exploring the library,...
Make applications cross-communicate using Apache Thrift! About This Book ...Learn to make your services ready for real-world applications by using stepwise ...Chapter 8: Advanced Usage of Apache Thrift
Master advanced usage, and bridge Swift and Objective-C Professional Swift is your guide to the future of OS X and iOS development. Table of Contents Part I: Building Applications with Swift Chapter ...
Dive into advanced usage, such extended client and server options Learn cluster sizing, tuning, and monitoring best practices Design schemas, copy tables, import bulk data, decommission nodes, and ...
Dive into advanced usage, such extended client and server options Learn cluster sizing, tuning, and monitoring best practices Design schemas, copy tables, import bulk data, decommission nodes, and ...
The book's tutorial style, step-by-step instruction will teach you everything you need to know to use the SAP Query tools, including its configuration, advanced usage, and integration to Microsoft....
taking you from beginner to advanced usage in a series of easy-to-follow steps. In this book, you'll learn how each component can be initialized in a basic default implementation and then see how ...
You will also learn about both basic and advanced usage of Laravel’s built-in methods, which will come in handy for your project. Also, you will learn how to extend the current libraries with the ...
Discover how easy it is to use tmux to collaborate remotely with others, and explore more advanced usage as you manage multiple tmux sessions, add custom scripts into the tmux status line, and ...
aims to take you from your first steps to an advanced usage of the JavaScript library of UI widgets and interaction helpers built on top of the hugely popular and easy-to-use jQuery. jQuery UI extends...
Advanced Usage -------------- For more control, use the ``QRCode`` class. For example:: import qrcode qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=...
Advanced Linker Techniques for Convenient and Efficient Memory Usage
It covers the basic and advanced usage of these services, testing and securing the serverless software, automating deployment, and more. Throughout this book, we will only use the Java programming ...
10.3.3. Advanced Profiler Usage 10.3.3.1. Filter by query elapsed time 10.3.3.2. Filter by query type 10.3.3.3. Retrieve profiles by query type 10.4. Zend_Db_Select 10.4.1. 简介 10.4.2. 同一表中...
5.1.3 Advanced Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 5.1.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.2 Java Clients (JDBC) . . . . . . . . . ...
Table of Contents Introduction....................................................................................................13 Code Examples.........................................................