Morphia 学习一 注解

Morphia 是一个针对Mongo和Java 对象转换的映射的轻量级ORM类型安全类库。

      1.简单易用,轻量级,一旦每一种类型通过反射获取将被缓存,性能比较好。

      2.Datastore和DAO<T,V>的抽象封装。

      3.快速的查询的支持,在类运行时进行校验。

      4.Mapping是基于注解而不是基于xml。

      5.针对Validation和Log的扩展。

      6.生命周期的控制。

      7.可以和Spring,Guice等DI框架整合。

      8.支持各种功能的扩展。

Entity注解

@Entity 
      value属性为DBConllection设置名称。必须有一个无参的默认构造方法,可以是public、protected、private等
      noClassnameStored属性默认为存储类名。如果只存储单一的实体对象并且关心数据库大小,不存储类名是安全的。
      保存类名的主要目的是在同一个链接中保存不同的实体对象,但是你想作为他们的基类或超类来读取。如果不在文档中保存类名,Morphia将不能正确的识别创建那个类。如:

 

  1. @Entity("animals") abstract class Animal { String name; }   
  2. @Entity("animals") Cat extends Animal { ... }   
  3. @Entity("animals") Dog extends Animal { ... }   
  4.   
  5. //And then performing the following query...  
  6. List<Animal> animals = ds.createQuery(Animal.class).asList();   


@Id
        @Id将值注解为MongoDB的唯一ID字段,MongoDB必须有一个唯一索引,mongo会自动生成id。如果使用其他类型,需要自己设置。

  1. @Id  
  2.     private ObjectId id;  


@Indexed
    当datastore.ensureIndexes() 方法调用时,mongoDB将该值生成索引。

  1. @Indexed(value=IndexDirection.ASC, name="upc", unique=true, dropDups=true)   
  2.     private String upcSymbol;  

    value :指定index的方向。默认ASC。 IndexDirection.ASC (ascending), IndexDirection.DESC (descending), IndexDirection.BOTH (both)
    name :指定index的名称。默认由mongoDB产生
    unique:是否为唯一索引。默认false。如果为true,插入重复值会报错。 
    dropDups:通知唯一索引删除重复值,只有第一条被保留,默认为false。

 

@Indexes&@Index
    复合indexes可以指定多个字段,该注解是class级别。例如下面代码指定user为默认升序同时date为降序(-表示DESC)

  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 date;  
  5.  String user;  
  6.  Record changedRecord;  
  7. } </span>  



如下表示可以找到最近变化的记录,该集合拥有两个复合indexes
@Transient 不将该字段保存到mongoDB@Property("feild_name") 指定该对象的属性映射到mongoDB的字段名称,默认为该对象的属性名。
@Serialized 字段被转换成二进制,并且被存储
@NotSaved 字段不会被保存,但是能被加载,良好的数据迁移
@AlsoLoad 该字段所以提供的名字都能被加载,良好的数据迁移
@Version 为Entity提供一个乐观锁,动态加载,不需要设置值

  1. @Entity  
  2. class Myclass {  
  3.    ...  
  4.    @Version Long v;  
  5. }  




@Embedded 创建一个类被嵌套在实体类中的情况下使用,例如,在Hotel类中 可能会有一个Address。Address是Hotel不可分割的一部分,没有ID, 并且不会被存储在分开的collection中。事实上被@Embedded注释的类不允许有@Id

  1. @Entity  
  2. public class Hotel {  
  3.   
  4.     @Id  
  5.     private String id;  
  6.   
  7.     private String name;  
  8.     private int stars;  
  9.   
  10.     @Embedded  
  11.     private Address address;  
  12.   
  13.     // ... getters and setters  
  14. }  
  15.   
  16. ...  
  17.   
  18. import com.google.code.morphia.annotations.Embedded;  
  19.   
  20. @Embedded  
  21. public class Address {  
  22.   
  23.     private String street;  
  24.     private String city;  
  25.     private String postCode;  
  26.     private String country;  
  27.   
  28.     // ... getters and setters  
  29. }  




@Reference 在数据库中引用另外一个文档,可以在多个Entity中引用同一个文档,注意被引用对象在被引用之前必须已经保存到mongoDB中。
      concreteClass: 指定具体的实体类。 
      ignoreMissing:  忽略任何不能解决的参考。 
      lazy:为参考创建一个代理,这个将在第一次调用时加载(类似Hibernate中的lazy属性) 
      value: 指定在Mongo中存储的属性名。默认使用对象的属性名保存到mongo中 


生命周期方法注解(delete没有生命周期事件)
@PrePersist save之前被调用,它可以返回一个DBObject代替一个空的
@PostPersist save到datastore之后被调用
@PreLoad 在Entity被映射到datastore对象之前被调用,可以对参数DBObject进行add/remove/change
@PostLoad     在Entity被映射之后被调用
@EntityListeners 指定外部生命周期事件实现类

    1. @EntityListeners(BackAccountWatcher.class)  
    2. class BankAccount {  
    3.   @Id String id;  
    4.   Date lastUpdated = new Date();  
    5. }  
    6.   
    7. class BankAccountWatcher{  
    8.     @PrePersist  
    9.     void PrePersist() {}                  
    10.     @PrePersist  
    11.     void PrePersistWithParam(BankAccount act) {act.lastUpdated = new Date();}  
    12.     @PrePersist  
    13.     DBObject PrePersistWithParamAndReturn(DBObject dbObj) {}  
    14.       
    15.     @PostPersist  
    16.     void postPersist() {}                  
    17.     @PostPersist  
    18.     void postPersistWithParam(DBObject dbObj) {}  
    19.       
    20.     @PreLoad  
    21.     void preLoad() {}                  
    22.     @PreLoad  
    23.     void PreLoadWithParam(DBObject dbObj) {}                
    24.     @PreLoad  
    25.     DBObject PreLoadWithParamAndReturn(DBObject dbObj) {}  
    26.       
    27.     @PostLoad  
    28.     void PostLoad() {}                  
    29.     @PreLoad  
    30.     void PostLoadWithParam(DBObject dbObj)  
posted on 2014-09-14 09:00  上校  阅读(7344)  评论(0编辑  收藏  举报