Why PlayMorphia
Morphia project has provided a lightweight and easy way to integrate power of MongoDB into Java application. There is absolute no problem to use Morphia directly in your Play application. So why do you bother with PlayMorphia module? Let’s see take a looks at a set of codes and understand why using PlayMorphia is a better approach when you are developing Play application:
Define entity model class
Given a very simple model class, PlayMorphia require the class to extends play.modules.morphia.Model
. You don’t need to extend any class if you are using pure Morphia. However, you must declare an @ID
field in your model class. In addition, the application developer needs to provide a way to ensure the model class is mapped.
CRUD and Query
PlayMorphia provides most natural way to do Create-Read-Update-Delete-Query operations on entity instance. Though it’s still quite easy to do that in pure Morphia also:
Statistics
Like all other interfaces provided, PlayMorphia provides a set of clean an simple interface for application developer to do statistics on their models:
TODO
More control on entity lifecycle management
In addition to existing Morphia lifecycle methods, PlayMorphia provides an new set of lifecycle annotation for application developer to use:
Annotation | Description | Comment |
---|---|---|
@OnLoad | called before an entity is filled with DB data | not used usually |
@Loaded | called after an entity is filled with DB data | good place to calculate derived properties, e.g. full name |
@OnAdd | called before an new entity is persisted into DB | good place to calculate db properties from derived properties |
@Added | called after an new entity is persisted into DB | good chance to do log/track etc |
@OnUpdate | called before an existing entity is persisted into DB | good place to calculate db properties from derived properties |
@Uploaded | called after an existing entity is persisted into DB | good place to do log/track etc |
@OnDelete | called before an existing entity is deleted | not used usually |
@Deleted | calledd after an existing entity is deleted | good place to do log/track etc |
@OnBatchDelete | called before a set of entities (specified by query) is deleted | good place to retrieve set of entity IDs;better to be declared on static method |
@BatchDeleted | called after a set of entities (specified by query) is deleted | good place to do log track;better to be declared on static method |
These annotations could be put on methods of your model class. There is no requirement on method visible scope (e.g. private, package, protected, public). Methods shall not return any object. There is shall be no argument declared in annotated methods except @OnBatchDelete
and @BatchDeleted
. The methods with “batch” annotations must have one argument: play.modules.morphia.Model.MorphiaQuery
. It’s recommended that “batch” annotated methods be declared as static methods, all other annotated methods be decalred as non-static one, but this is not required.
In addition to the above annotations, PlayMorphia provides two events distribution mechanisms to enable application and/or plugin developers to inject their logic interactive with entity lifecycles. Please refer to Lifecycle events for details