Naked Objects provides a number of well-defined callbacks into the domain object lifecycle. These are implemented as facets that search for well-defined methods:
created()
(transient) domain object instantiated and initialized (dependencies injected, non-optional properties set to default values)
persisting()
,
persisted()
transient domain object about to be persisted / has been persisted
loading()
,
loaded()
persistent domain object about to be loaded / has been loaded
updating()
,
updated()
persistent domain object about to be updated / has been updated
removing()
,
removed()
persistent domain object about to be removed / has been removed (that is, deleted)
These callbacks are supported by all object store implementations. A typical example is to capture the ID for use within a domain object.
In addition, the JPA specification defines a number of annotations that also define callbacks into the persistence lifecycle. Because these are implemented by the JPA provider (Hibernate), they won't be portable across different object stores. However, they may well be of use in production code.
The callbacks provided are (methods annotated using):
@PostLoad
@PrePersist
,
@PostPersist
@PreUpdate
,
@PostUpdate
@PreRemove
,
@PostRemove
For more on these callbacks, see for example this article from Oracle's technical documentation.
Alternatively, the @EntityListeners
annotation can be used to specify another class to be notified.