-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Auditing with Events
PostPosted: Thu Aug 03, 2006 8:12 am 
Newbie

Joined: Thu Aug 03, 2006 6:02 am
Posts: 2
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.