Callbacks on Entity Classes




Callbacks on Entity Classes

You can have an entity bean instance register for a callback on any of these life cycle events by annotating a public, private, protected, or package-protected method on the bean class. This method must return void , throw no checked exceptions, and have no arguments.

@Entity
public class Cabin {

   ...
 
   @PostPersist void afterInsert( ) {
    ...
   }

   @PostLoad void afterLoading( ) {
    ...
   }

}

When an event is triggered on a particular managed entity instance, the entity manager will invoke the appropriate annotated method on the xsentity bean class.

If you are annotation-averse, you can hook into these events by using the <pre-persist>, <post-persist>, <pre-update>, <post-update>, <pre-remove>, <post-remove>, and <post-load> subelements of <entity> in the ORM mapping deployment descriptor:

<entity class="com.titan.domain.Cabin">
   <post-persist name="afterInsert"/>
   <post-load name="afterLoading"/>
</entity>

These subelements have one attribute called name that takes the name of the method you want to invoke when the callback event happens.