Recently i was working on a project, where we were using "tracked" objects. This is a kind of versioning mechanism, implemented on DB level using triggers.
When i was starting to implement tracked objects in Hibernate, i've noticed that there are few properties that every tracked object has:
- T_PK - primary key in table with versions
- T_START - starting moment for transcation
- T_END - end moment for transcation
The idea was to add this stuff to some other Hibernate entity to not have it duplicated in every mapping file. I remembered that <class> element could have "abstract" attribute set to true, so i can just extend it and be cool. Not really :)
There are four types of inheritance relation used in Hibernate, well-described here:
http://simsonlive.wordpress.com/2008/03/09/how-inheritance-works-in-hibernate/
http://javavibes.wordpress.com/2009/06/22/hibernate-inheritance/
- One table per concrete class
That sounded good at first glance, but it allows the reusing only on java side, and i don't care about it too much because it is auto generated.
- One table per concrete class with union-subclass mapping
In this case all versed objects should be a <union-subclass>, plus they have to be in one hibernate mapping file with the parent class.
Not sure if it is ok... It may produce what i want, but i have all the versed objects in one place. This option is supposed to be used for polymorphic queries, and i want to just have all the fields in one place.
- One table per class hierarchy
It is just not suitable at all, we have a bunch of tables instead of one.
- Implicit polymorphism
Again, not suitable because java objects are generated.
After i took a look into the possibility of using one aggregated object for versioning
http://docs.jboss.org/hibernate/stable/core/reference/en/html/components.html
Got a few problems here also:
- ID couldn't be a part of aggregated object
- It is copy-pasting too
- hbm2java produces strange things when component is in place (maybe i was doing something wrong, i haven't dig deep there)
Then i searched the Hibernate forum for topics about simple reusing of mappings, and i've found something interesting:
Turns out copy-pasting all these versioning information is the best way to do things? Looks like this.