Morphia for MongoDB

Source: Internet
Author: User
MorphiaIs a lightweight type-safe Library for mapping Java objects to/from
MongoDB:
Morphia is a secure and lightweight Java persistence architecture designed for MongoDB.

1. To use morphia, You need to rely on the following jars:


MongoDB Java driver

Morphia release (select different versions based on driver versions)

Optional (but needed if you use Maven)

Cglib

Proxytoys

If Maven is used in the project, you must add

<dependency>         <groupId>com.google.code.morphia</groupId>         <artifactId>morphia</artifactId>         <version>###</version> </dependency> <!-Optional Jars (for certain features) but required by maven for bulding. --> <dependency>         <groupId>cglib</groupId>         <artifactId>cglib-nodep</artifactId>         <version>[2.1_3,)</version>         <type>jar</type>         <optional>true</optional> </dependency> <dependency>         <groupId>com.thoughtworks.proxytoys</groupId>         <artifactId>proxytoys</artifactId>         <version>1.0</version>         <type>jar</type>         <optional>true</optional> </dependency>
2. Spring Test-based basic morphia operation @ runwith (springjunit4classrunner. Class)
@ Contextconfiguration (locations = {"classpath: spring-beans-test.xml "})
Public class morphiadaotest {
@ Resource
Resourcemethoddao;
@ Resource
Parameterdefinitiondao;
 
@ Before
Public void checknull (){
Assert. notnull (resourcemethoddao );
} Public void addobject (){
Resourcemethod method = new resourcemethod ();
Method. setname ("product get ");
Method. setjsbody ("javascript: void (0 )");
Method. setcreator ("Liu ");
Method. setid (4 );
Method. setcreatedtime (new date ());
List list = getresoucedefinition ();
Method. setparameters (list );
Key <resourcemethod> id = resourcemethoddao. Save (method );
Assert. notnull (ID );
} Public void query (){
Query <resourcemethod> query = resourcemethoddao. createquery ();
// Query. Field ("creator"). Equal ("Liu ");
Parameterdefinition object = new parameterdefinition ();
Object. setid (1 );
Query. Field ("Parameters"). hasthiselement (object );

// Query <resourcemethod> query = resourcemethoddao. createquery (). filter ("ID>", 0 ). order ("-ID"); // If the query ID is greater than 0, // sort by ID in descending order
List list = resourcemethoddao. Find (query). aslist ();
System. Out. println (list );
}
 
Public void Delete (){
// Resourcemethod res = new resourcemethod ();
Query <resourcemethod> query = resourcemethoddao. createquery ();
Query. Field ("creator"). Equal ("Liu ");
Resourcemethoddao. deletebyquery (query );
// Resourcemethoddao. deletebyid (ID)
// Resourcemethoddao. Delete (RES); // The object must contain at least the ID
}
@ Test
Public void Update (){
Query <resourcemethod> query = resourcemethoddao. createquery ();
Query. Field ("creator"). Equal ("Alan ");
Updateoperations <resourcemethod> Ops = resourcemethoddao. createupdateoperations (). Set ("creator", "long"). Inc ("resourcetype", 12 );
Resourcemethoddao. Update (query, OPS );
// Resourcemethoddao. updatefirst (query, OPS );
}
Private list <parameterdefinition> getresoucedefinition (){
List <parameterdefinition> List = new arraylist <parameterdefinition> ();
Parameterdefinition para = new parameterdefinition ();
Para. setid (1 );
Para. setname ("name ");
Para. settype (fieldtype. String );
Parameterdefinitiondao. Save (para );
List. Add (para );

Parameterdefinition para1 = new parameterdefinition ();
Para1.setid (2 );
Para1.setname ("value ");
Para1.settype (fieldtype. String );
Parameterdefinitiondao. Save (para1); // The @ reference method is used for reference. Therefore, you must ensure that reference data exists in the Word Table before inserting.
List. Add (para1 );
Return list;
} Dao integrates basicdaopublic class resourcemethoddao extends basicdao <resourcemethod, integer> {
Protected resourcemethoddao (datastore DS ){
Super (DS );
}
} Entity @ entity
Public class resourcemethod implements serializable {Private Static final long serialversionuid =-2260371987627421226l; @ ID
Int ID;/** method name */
String name;/** resource type */
Int resourcetype;/** return data structure type */
Datastructure returnstructure;/** system method */
Boolean system;/** JS implementation method, effective when system = false */
String jsbody;/** creation date */
Date createdtime;/** creator */
String creator;/** update date */date updatedtime;/** updated */
String updator;/** method parameter list */
@ Reference
List <parameterdefinition> parameters; ......} @ entity
Public class parameterdefinition implements serializable {
@ ID
Integer ID;/** parameter name */
String name;/** parameter type */
Fieldtype type;...} contents of the spring-beans-test.xml <? XML version = "1.0" encoding = "GBK"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd> <bean id = "mongo" class = "com. MongoDB. Mongo">
<Constructor-Arg Index = "0" value = "219. 239. *. *"/>
<Constructor-Arg Index = "1" value = "27017"/>
</Bean>
<Bean id = "morphia" class = "com. TMG. rescenter. Core. Dao. morphiabean">
<Property name = "mongo" ref = "mongo"/>
<Property name = "databasename" value = "alan_test"/>
</Bean>
<Bean id = "datastore" factory-bean = "morphia" factory-method = "getdatastore"/> <! -- The following is the definition of Dao beans for all business classes --> <bean id = "parameterdefinitiondao" class = "com. TMG. rescenter. Core. Dao. parameterdefinitiondao">
<Constructor-Arg ref = "datastore"/>
</Bean>
<Bean id = "resourcemethoddao" class = "com. TMG. rescenter. Core. Dao. resourcemethoddao">
<Constructor-Arg ref = "datastore"/>
</Bean>
</Beans> 3. Note that @ embedded references other objects in an object, but does not create a reference table or create an external Association, which is redundant, however, you do not need to consider the consistency between the master table and the reference table. On the associated entities, Mark @ embedded @ reference to reference other objects in an object. First, you need to create reference table data, the external association is established by ID. Considering the consistency between the master table and the reference table, the Association object is marked with @ entity as the above query. field ("creator "). equal ("Liu"); and query. filter ("creator =", "Liu"); it has the same effect. For more information, see the above Code, or log on to http://code.google.com/p/morphia/ to use the detailed or source code.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.