We saw session bean callback methods in Chapter 2. Callback methods are also provided for events in an entity's lifecycle. The callback events are:
PostLoad
PrePersist
PostPersist
PreRemove
PostRemove
PreUpdate
PostUpdate
An entity can define a method corresponding to one or more of these events by prefixing the method with a corresponding annotation. So a PostLoad
method would be prefixed by @PostLoad
and so on.
The PostLoad
callback method is invoked by the entity manager after an entity has been loaded by a query, or after a refresh
operation. For example, in the Customer entity we could add a PostLoad
method as follows:
@PostLoad public void postLoad(){ System.out.println("customer.postLoad method invoked"); }
Note we do not have to name our method postLoad()
. Any name is acceptable provided the method returns a void
, has no arguments and does not throw a checked exception. This applies to any of the entity lifecycle callback methods.
The PrePersist...