|
LifecycleMethods
A list of Annotation to decorate your Entity with for lifecycle events.
Lifecycle Method AnnotationsThere are various annotations which can be used to register callbacks on certain lifecycle events. These include Pre/Post-Persist (Save), and Pre/Post-Load.
See the AllAnnotation for a full list of the annotations supported. ExamplesHere is a one of the test classes: http://code.google.com/p/morphia/source/browse/trunk/morphia/src/test/java/com/google/code/morphia/TestDatastore.java#82 All parameters and return values are options in your implemented methods. Simple @PrePersistHere is a simple example of an entity that always saves the Date it was last updated at.
class BankAccount {
@Id String id;
Date lastUpdated = new Date();
@PrePersist void prePersist() {lastUpdated = new Date();}
}
EntityListernersIn addition, you can separate the lifecycle event implementation in an external class, or many.
@EntityListeners(BackAccountWatcher.class)
class BankAccount {
@Id String id;
Date lastUpdated = new Date();
}
class BankAccountWatcher{
@PrePersist void prePersist(BankAccount act) {act.lastUpdated = new Date();}
}
No Delete SupportBecause deletes are usually done with queries there is no way to support a Delete lifecycle event. If, or when, server-side triggers are enabled there may be some support for this, but even then it will be hard to imagine how this would logically fit. | |
Lifecycle methods appear to be able to be declared with zero, one, or two arguments:
@<LifeCycleEventAnnotation> DBObject lifecycleMethod(DBObject dbObj, Mapper mapper);
- with/ arguments left off the right as required.
The return value appears to be utilized as follows:
'EntityListerners?' -> EntityListeners?
@link(See the AllAnnotation? for a full list of the annotations supported.) -> http://code.google.com/p/morphia/wiki/AllAnnotations