Morphia learning Annotation

Source: Internet
Author: User

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
  1. @ Entity ("Animals") abstract class animal {string name ;}
  2. @ Entity ("Animals") cat extends animal {...}
  3. @ Entity ("Animals") Dog extends animal {...}
  4. // And then locking Ming the following query...
  5. 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
  1. @ ID
  2. Private objectid ID;


@ Indexed
When the datastore. ensureindexes () method is called, MongoDB generates an index for this value.

[Java]View plaincopy
  1. @ Indexed (value = indexdirection. ASC, name = "UPC", unique = true, dropdups = true)
  2. 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
  1. <Span style = "font-size: 10px;"> @ entity // This is require to know where the indexes are to be created
  2. @ Indexes (@ index ("user,-date "))
  3. Public class changelog {
  4. Date;
  5. String user;
  6. Record changedrecord;
  7. } </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
  1. @ Entity
  2. Class myclass {
  3. ...
  4. @ Version long V;
  5. }




@ 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
  1. @ Entity
  2. Public class hotel {
  3. @ ID
  4. Private string ID;
  5. Private string name;
  6. Private int stars;
  7. @ Embedded
  8. Private address;
  9. //... Getters and setters
  10. }
  11. ...
  12. Import com. Google. Code. morphia. Annotations. Embedded;
  13. @ Embedded
  14. Public Class address {
  15. Private string Street;
  16. Private string city;
  17. Private string postcode;
  18. Private string country;
  19. //... Getters and setters
  20. }




@ 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
    1. @ Entitylisteners (backaccountwatcher. Class)
    2. Class bankaccount {
    3. @ ID string ID;
    4. Date lastupdated = new date ();
    5. }
    6. Class bankaccountwatcher {
    7. @ Prepersist
    8. Void prepersist (){}
    9. @ Prepersist
    10. Void prepersistwithparam (bankaccount Act) {act. lastupdated = new date ();}
    11. @ Prepersist
    12. Dbobject prepersistwithparamandreturn (dbobject dbobj ){}
    13. @ Postpersist
    14. Void postpersist (){}
    15. @ Postpersist
    16. Void postpersistwithparam (dbobject dbobj ){}
    17. @ Preload
    18. Void preload (){}
    19. @ Preload
    20. Void preloadwithparam (dbobject dbobj ){}
    21. @ Preload
    22. Dbobject preloadwithparamandreturn (dbobject dbobj ){}
    23. @ Postload
    24. Void postload (){}
    25. @ Preload
    26. Void postloadwithparam (dbobject dbobj)

Morphia learning Annotation

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.