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.  [ 3 posts ] 
Author Message
 Post subject: Tracking Historic changes
PostPosted: Thu Jun 22, 2006 5:11 am 
Newbie

Joined: Mon Apr 18, 2005 1:10 pm
Posts: 4
Hibernate version:
3.0.2

I am trying to track changes that are made to an object and also track these changes in the database and was wandering if this is possible in hibernate. So for example if an object is updated I want to change the status of the current object and the effective_to date for the object and create a new object with the new values. That way I am creating a history of all the changes that have taken place in the database. Is this possible to do in hibernate. Searching the forums it appears that it is possible to use interceptors but they do not seem to make the changes in the database.

Any pointers esp if this is feasible would be appreciated.

Sonik


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 8:18 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
You should be able to do this with an Interceptor -- I've got an implementation of an Interceptor that updates an "activiity" table based on changes made to several other tables. It's a little tricky -- check out the Hibernate WIKI for examples.

You might be able to do this with Events. Events were new in H3. I haven't worked with them because I started with H2.

While you have probably considered this, are you sure what the application to manage the "history"? You could enforce the creation of history in all situations and would probably get better performance if done with db triggers.

Curtis ...

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 8:45 am 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
I'm using annotations (EJB 3) and I have something similar to what you want. While it is possible to do that with Entity Listeners, you will face some problems. Here is a piece of advice if you are going that way:
  • Set all queries that are used (directly and indirectly) by the listeners to flush mode commit (Query.setFlushMode(FlushModeType.COMMIT)). Not doing so will most likely raise an exception.
  • Watch for many-to-one relationships with optional = true. I am not being able to query these fields separately if they are null.
  • Try not to initialize a second instance of the entity you are listening to (in order to get the currently persisted values), specially if that entity has lazy one-to-many relationships. You will also get an exception.

So I would recommend you to do your history in your business methods. Maybe you can generalize so it can be called for any entity, but using entity listeners does not sound like a good idea for the time being.

_________________
Henrique Sousa
Don't forget to rate useful responses


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

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.