MorphiaIs a lightweight ORM type security class library mapped to Mongo and Java object conversion.
1. Easy to use and lightweight. Once each type is obtained through reflection, it will be cached, and the performance is better.
2. Abstract encapsulation of datastore and Dao <t, V>.
3. Quick query support, which is verified during class running.
4. Mapping is based on Annotations rather than XML.
5. extended for validation and log.
6. lifecycle control.
7. Integration with DI frameworks such as spring and guice.
8. Supports expansion of various functions.
Entity Annotation
@ Entity
The value attribute is the name of dbconllection. There must be a default constructor without parameters, such as public, protected, and private.
By default, the noclassnamestored attribute is the storage class name. If you only store a single object and care about the database size, it is safe not to store class names.
The main purpose of saving class names is to save different object objects in the same link, but you want to read them as their base class or superclass. If the class name is not saved in the document, morphia cannot correctly identify the class to be created. For example:
[Java]View plaincopy
- @ Entity ("Animals") abstract class animal {string name ;}
- @ Entity ("Animals") cat extends animal {...}
- @ Entity ("Animals") Dog extends animal {...}
- // And then locking Ming the following query...
- List <animal> animals = Ds. createquery (animal. Class). aslist ();
@ ID
@ ID: annotate the value as the unique ID field of MongoDB. MongoDB must have a unique index, and Mongo will automatically generate an ID. If other types are used, you need to set them yourself.
[Java]View plaincopy
- @ ID
- Private objectid ID;
@ Indexed
When the datastore. ensureindexes () method is called, MongoDB generates an index for this value.
[Java]View plaincopy
- @ Indexed (value = indexdirection. ASC, name = "UPC", unique = true, dropdups = true)
- Private string upcsymbol;
Value: Specifies the direction of the index. The default value is ASC. Indexdirection. ASC (ascending), indexdirection. DESC (descending), indexdirection. Both (both)
Name: Specifies the index name. Generated by MongoDB by default
Unique: whether it is a unique index. The default value is false. If this parameter is set to true, an error is returned when duplicate values are inserted.
Dropdups: notifies the unique index to delete duplicate values. Only the first index is retained. The default value is false.
@ Indexes & @ Index
Compound indexes can specify multiple fields, which are class-level annotations. For example, the following code specifies that the user is in ascending order by default, and the date is in descending order (-Indicates DESC)
[Java]View plaincopy
- <Span style = "font-size: 10px;"> @ entity // This is require to know where the indexes are to be created
- @ Indexes (@ index ("user,-date "))
- Public class changelog {
- Date;
- String user;
- Record changedrecord;
- } </Span>
The following shows the records of recent changes. This set has two composite indexes.
@ Transient does not save this field to [email protected] ("feild_name") to specify the field name mapped to MongoDB for this object. The default value is the property name of this object.
@ Serialized the field is converted to binary and stored
@ Notsaved the field will not be saved, but it can be loaded, and data migration is good.
@ Alsoload the field so the provided names can all be loaded, good data migration
@ Version provides an optimistic lock for entity, which can be dynamically loaded and does not need to be set.
[Java]View plaincopy
- @ Entity
- Class myclass {
- ...
- @ Version long V;
- }
@ Embedded create a class that is nested in the object class. For example, there may be an address in the hotel class. Address is an integral part of a hotel. It does not have an ID and is not stored in a separate collection. In fact, @ ID is not allowed for Classes annotated by @ embedded.
[Java]View plaincopy
- @ Entity
- Public class hotel {
- @ ID
- Private string ID;
- Private string name;
- Private int stars;
- @ Embedded
- Private address;
- //... Getters and setters
- }
- ...
- Import com. Google. Code. morphia. Annotations. Embedded;
- @ Embedded
- Public Class address {
- Private string Street;
- Private string city;
- Private string postcode;
- Private string country;
- //... Getters and setters
- }
@ Reference references another document in the database. You can reference the same document in multiple entity documents. Note that the referenced object must be saved to MongoDB before being referenced.
Concreteclass: Specifies the object class.
Ignoremissing: ignore any reference that cannot be resolved.
Lazy: Create a proxy for reference. This will be loaded during the first call (similar to the lazy attribute in hibernate)
Value: Specifies the attribute name stored in Mongo. By default, the attribute names of objects are saved to Mongo.
Life cycle method annotation (delete does not have a life cycle event)
@ Prepersist is called before saving. It can return a dbobject instead of an empty
@ Postpersist save to datastore is called
@ Preload is called before entity is mapped to a datastore object. You can Add/Remove/change the parameter dbobject.
@ Postload is called after entity is mapped
@ Entitylisteners specifies the external lifecycle event implementation class
[Java]View plaincopy
- @ Entitylisteners (backaccountwatcher. Class)
- Class bankaccount {
- @ ID string ID;
- Date lastupdated = new date ();
- }
- Class bankaccountwatcher {
- @ Prepersist
- Void prepersist (){}
- @ Prepersist
- Void prepersistwithparam (bankaccount Act) {act. lastupdated = new date ();}
- @ Prepersist
- Dbobject prepersistwithparamandreturn (dbobject dbobj ){}
- @ Postpersist
- Void postpersist (){}
- @ Postpersist
- Void postpersistwithparam (dbobject dbobj ){}
- @ Preload
- Void preload (){}
- @ Preload
- Void preloadwithparam (dbobject dbobj ){}
- @ Preload
- Dbobject preloadwithparamandreturn (dbobject dbobj ){}
- @ Postload
- Void postload (){}
- @ Preload
- Void postloadwithparam (dbobject dbobj)
Morphia learning Annotation