I need to do versioning on (simple) Java object graphs stored in a document-oriented database (MongoDB). For relational databases and Hibernate, I discovered Envers and am very amazed about the possibilities. Is there something similar that can be used with Spring Data Documents?
I found this post outlining the thoughts I had (and more…) about storing the object versions, and my current implementation works similar in that it stores copies of the objects in a separate history collection with a timestamp, but I would like to improve this to save storage space. Therefore, I think I need to implement both a "diff" operation on object trees and a "merge" operation for reconstructing old objects. Are there any libraries out there helping with this?
Edit:
Any experiences with MongoDB and versioning highly appreciated! I see most probably there won't be a Spring Data solution.
Best Answer
This is how I ended up implementing versioning for MongoDB entities. Thanks to the StackOverflow community for helping!
equals
method of the entities so that it tests for the database primary key and not the sub properties. (Otherwise, JavaObjectDiff will not recognize property changes in collection elements.)Here are the entities I use for versioning (getters/setters etc. removed):
Here is the saveChangeHistory operation:
This is how it looks like in MongoDB:
EDIT: Here is the Visitor code: