Java Reference
In-Depth Information
Type Name
Listener
pre-delete
PreDeleteEventListener
pre-insert
PreInsertEventListener
pre-load
PreLoadEventListener
pre-update
PreUpdateEventListener
refresh
RefreshEventListener
replicate
ReplicateEventListener
save-update
SaveOrUpdateEventListener
So, for example, your listener for the SaveOrUpdateEvent is mapped to the type name
save-update , must implement the SaveOrUpdateEventListener interface, and would nor-
mally have been implemented by the DefaultSaveOrUpdateEventListener class. It is wise to
follow a similar convention with your own naming, so your mapping file listener entry
might read like this:
<listener type="save-or-update"
class="com.hibernatebook.advanced.BookingSaveOrUpdateEventListener"/>
Alternatively, a programmatic registration of the same event would be given thus:
Configuration config = new Configuration();
config.setListener("save-update", new BookingSaveOrUpdateEventListener());
Because they override the default behavior, events are suitable for situations in which
you want to fundamentally change the Session 's behavior—particularly if you want to pre-
vent a certain event from being processed. Probably the best example of this requirement
is in authorizing access to the database, and, in fact, Hibernate provides a set of event lis-
teners for just this purpose. The four events listeners in question override the PreDelete ,
PreUpdate , PreInsert , and PreLoad listeners. The logic in each case (in pseudocode) runs
something like this:
if( user does not have permission ) throw RuntimeException
Invoke default listener…
Because events are invoked in the same thread as the user's call to the session, the result
of an exception in the first step will be an exception (actually a security exception) as the
unprivileged user carries out the relevant operation.
To enable policy configuration of security, you would add the following:
<listener type="pre-delete"
class="org.hibernate.secure.JACCPreDeleteEventListener"/>
<listener type="pre-update"
class="org.hibernate.secure.JACCPreUpdateEventListener"/>
<listener type="pre-insert"
class="org.hibernate.secure.JACCPreInsertEventListener"/>
<listener type="pre-load"
class="org.hibernate.secure.JACCPreLoadEventListener"/>
Search WWH ::




Custom Search