Tracking changes to db fields while using ORM
I want to track the changes made to every column in a particular table. I thought of storing the changes in another table with the following columns: id, field, and data
where field
(string value) is the column in the target table that has been altered and data
is the new value of that field.
When returning the data set, I thought of retrieving the original data from the target table and the changed values from the table tracking changes, then using java's reflection capabilities to set the changed fields' values to the newest changed value.
Based on the above I have the following questions:
- Does my approach makes sense? Is there a better way to achieve the desired results?
- I'm using JPA 2 to handle ORM, are there any ORM features that ca开发者_如何转开发n be helpful in achieving the end goal?
Thanks
Check Hibernate envers and Eclipselink historical session for entity auditing.. JPA 2.0 spec does not have anything related to entity auditing..
Your approach is fine. You may need many "data" fields to cover various data types (e.g date, text, longtext).
What I do to solve this problem (but also easily provide rollback capabilities) is to use xstream and a text diff engine. I use xstream to grab an xml snapshot of the pojo, then apply the user's edit and then grab another. I diff the two snapshots and store the diff text. Also with some effort I can pretty print the modification in a wiki style.
精彩评论