MorphiaMongoDB 对象映射框架

联合创作 · 2023-09-30 03:31

Morphia 是一个轻量级的类型安全的 Java 类库,用来将在 MongoDB 和 Java 对象之间进行映射。

@Entity("employees")
class Employee {
  @Id ObjectId id; // auto-generated, if not set (see ObjectId)
  String firstName, lastName; // value types are automatically persisted
  Long salary = null; // only non-null values are stored 

  @Embedded Address address;

  @Reference Employee manager; // refs are stored*, and loaded automatically
  @Reference List<Employee> underlings; // interfaces are supported

  @Serialized EncryptedReviews; // stored in one binary field 
 
  @Property("started") Date startDate; //fields can be renamed
  @Property("left")Date endDate;

  @Indexed boolean active = false; //fields can be indexed for better performance
  @NotSaved string readButNotStored; //fields can read, but not saved
  @Transient int notStored; //fields can be ignored (load/save)
  transient boolean stored = true; //not @Transient, will be ignored by Serialization/GWT for example.

  //Lifecycle methods -- Pre/PostLoad, Pre/PostSave...
  @PostLoad void postLoad(DBObject dbObj) { ... }
}
浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报