Keeping a history of data in a single table in hibernate, where all versions are treated as first class objects.
That is to say that every record in the database will be immutable. New records will be created with a new version number, but the old versions will never change. They are kept as the history of changes.
When querying objects from hibernate ensure that they are initialised, and then detach them from the session:
Then when saving the objects, ensure that the ID is null, and call the
session.save(businessObject) to save them.
//set the id to null to ensure that a new record is stored in the database.
The final part of the puzzle is to ensure that the class has it’s persistence properties set correctly, either by setting the annotations or configuring the xml file.
The important settings are:
- To help prevent updates, and improve performance set the mutable attribute on the class to false.
- The collection objects on the class have the lazy attribute set to false (ensuring that they are loaded when the object is retrieved, and the evicted object will work).
Following these steps will provide you with a versioned business object. It will be possible to retrive, and store the object to the database using the Hibernate search functionality.