Hibernate version: 3.2 RC2
Name and version of the database you are using: DB2 UDB 8.3
I tried to figure out how to implement auditing with Hibernate's event system. The goal is to insert an entry with who/when/what changed for every successful insert/update/delete on certain tables. The solutions proposed so far in this forum use Interceptors.
My problem is that I don't know which events I should use. It seems that Pre/PostInsert/Update/Delete-Events are triggered regardless whether the transaction surrounding the appropriate statements failed or not.
Example code:
Code:
session.beginTransaction();
Team team = new Team();
team.setShortName("HSV");
team.setCity("Harburg");
session.save(team);
team.setCity("Hamburg");
session.update(team);
session.getTransaction().rollback();
This code will have no real effect on the database at all, since the transaction never succeeds. This code also should not generate any audit logs since no data is modified at all.
The configuration in hibernate.cfg.xml:
Code:
<event type="post-insert">
<listener
class="entity.JournalEventListener" />
</event>
<event type="post-update">
<listener
class="entity.JournalEventListener" />
</event>
<event type="post-delete">
<listener
class="entity.JournalEventListener" />
</event>
<event type="pre-insert">
<listener
class="entity.JournalEventListener" />
</event>
<event type="pre-update">
<listener
class="entity.JournalEventListener" />
</event>
<event type="pre-delete">
<listener
class="entity.JournalEventListener" />
</event>
The class entity.JournalEventListener implements the corresponding interfaces and print a log statement for every triggered event.
Example:
Code:
public void onPostDelete(PostDeleteEvent event) {
System.out.println("post delete "
+ event.getEntity().getClass().getName());
}
The log of the code above reads like this:
Code:
pre insert entity.Team
Hibernate: insert into team (id, shortName, city) values (default, ?, ?)
Hibernate: values identity_val_local()
post insert entity.Team
1. How can I reliably check whether an entity is really inserted/updated/deleted?
2. I also tried to implement FlushEntityEventListener, but apparently this event isn't triggered for every insert/update/delete. Is there a comprehensive documentation about when exactly an event is called?
3. Is there any event which is triggered whenever a transaction fails? I thought Hibernate must know which entities have been modified and must roll back these changes?