MongoDB對象映射框架 Morphia
Morphia 是一個輕量級的類型安全的 Java 類庫,用來將在 MongoDB 和 Java 對象之間進行映射。
- Easy to use, and very lightweight; reflection is used once per type and cached for good performance.
- Datastore and DAO<T,V> access abstractions, or roll your own...
- Type-safe, and Fluent Query support with (runtime) validation
- Annotations based mapping behavior; there are no XML files.
- Extensions: Validation (jsr303), and SLF4J Logging
@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) { ... }
}</pre>