`
leonzhx
  • 浏览: 796802 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    simple-tags

    * 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...

    SAP query reporting

    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....

    gorm文档_英文.pdf

    gorm文档的 Advanced Usage 部分讲解了gorm库的高级用法,包括错误处理、事务处理、Raw SQL & SQL Builder、Generic database interface sql.DB、Composite Primary Key、Overriding Logger 等内容。 7. ...

    Learning Less.js(PACKT,2015)

    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,...

    Professional.Swift.1119016770

    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 ...

    HBase.The.Definitive.Guide.2nd.Edition

    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 ...

    SAP Query Reporting (chm文件)

    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....

    GNU Wget 1 16 2 English

    7. 示例(Examples):这里提供了Wget的基本使用(Simple Usage)、高级使用(Advanced Usage)以及非常高级的使用(Very Advanced Usage)的示例。 8. 其他(Various):介绍了Wget的代理设置(Proxies)、分发...

    selenium-2.33.0

    8. `Test Design Considerations — Selenium Documentation_files`、`WebDriver Advanced Usage — Selenium Documentation_files`、`Selenium-IDE — Selenium Documentation_files`:这些是对应HTML文档的资源...

    Problem Solving with C++ (7th edition)

    - **Namespaces and Templates**: Further discussion of namespaces and templates, including advanced usage scenarios. **Programming Project 13.6 and 13.9**: These projects likely involve using advanced...

    Laravel Application Development Blueprints

    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 ...

    tmux.2.Productive.Mouse-Free.Development.epub

    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 ...

    c# http post get

    ### Advanced Usage #### Sending POST Data ```csharp string postData = "username=john&password=demo"; string response = client.OpenRead("http://example.com/login", postData); Console.WriteLine...

    生成QR二维码

    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=...

    XDoclet in Action

    The second part focuses on advanced usage scenarios of XDoclet within enterprise Java development, including integration with various frameworks and technologies. #### Chapter 3: XDoclet and ...

    Building Serverless Architectures-Packt Publishing(2017).pdf

    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 ...

Global site tag (gtag.js) - Google Analytics