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) { ... } }