• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

History of object in Hibernate

 
Johann Spitz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a hard time trying to implement a persistence mechanism in Hibernate, that lets me see the changes that were made to my objects in some kind of a history table.

I tried the following (pseudocode):

interface HistoryPojo{
Long Id
createUser Id
createDate date
changeUser Id
changeDate date
Long parentId
}

Every Object with a history implements this interface. Now everytime a User wants to make changes, I want to copy the old value (clone the bean and store as a new object in the db) set its id = null and its parentId to the id of the current object - and only modify the current object.
So far i've been successful with that.

But then there are tables with one-to-many collections, which fail with a
"net.sf.hibernate.HibernateException: Found shared references to a collection" exception. Now, clearly - hibernate doesn't like my style of doing a history.

I would break a lot of constraints with this, or I would have to copy all references to my object with new values.

Does somebody have a working solution for this history problem?
Your help is greatly appreciated!

Best regards Johann
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've chosen to implement this as a trigger in our database until I address this in our recent port from EJBs as it's trivial there and is very performant. Here is the solution I intend to implement.

I won't change the database when I do this in Hibernate. I have a separate history table for each domain object: device has device_log for example. The sufficient reason for this is that the primary key column (Id) does not allow null values. Foreign keys just add to the nightmare, for do you add a duplicate column for each one like old_device_id?

Create and map a history class for each class I need to track. You rarely need to track every object's complete history, so this shouldn't be a large burden. Sure it's a nice to have, but is it worth the decreased performance, increasedcomplexity, and increased data needs in all cases?

Maybe Hibernate 3 has a more automatic solution to this problem.
 
Johann Spitz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, thats sounds feasible to me too. It sure helps keeping down the complexity of the object relations as long as you do not permit your users to delete data that is otherwise still referenced in the history.

On the other hand, what happens with your objects in the history tables? Do they still have references to the original objects in the "current" table, or do they only reference objects in the history tables? If the latter is true, then it's still kind of burdensome to do this all without some kind of automation.

Also, wouldn't this require not only a new Table, but also a new Pojo class?

Wow, I really think there should be some kind of Framework for this. There has got to be a clean way.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Johann Spitz:
What happens with your objects in the history tables? Do they still have references to the original objects in the "current" table, or do they only reference objects in the history tables?

Also, wouldn't this require not only a new Table, but also a new Pojo class?
There are no foreign keys in the database for the history tables, nor does the duplicate POJO class have any collections or references. Each class with history would have a separate POJO class detached from all classes in the object graph.
Wow, I really think there should be some kind of Framework for this. There has got to be a clean way.
You are correct; this requires a lot of work. This is why I make sure as few classes are tracked this way as are truly necessary.

You could always use triggers if your database supports them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic