mangodb数据库框架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将不能正确的识别创建那个类。如:

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

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

@Id  
    private ObjectId id;  

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

@Indexed(value=IndexDirection.ASC, name="upc", unique=true, dropDups=true)   
    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)

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

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

@Entity  
class Myclass {  
   ...  
   @Version Long v;  
}  

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

 
@Entity  
public class Hotel {  
  
    @Id  
    private String id;  
  
    private String name;  
    private int stars;  
  
    @Embedded  
    private Address 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 在数据库中引用另外一个文档,可以在多个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 指定外部生命周期事件实现类

@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)  
原文地址:https://www.cnblogs.com/ss561/p/4682417.html