This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Is there a automatic way to detect if an object has changed since it has been fetched from the database.
I want to persist a whole object tree. But I must detect which objects of the tree have changes and then set the date and user on them.
You can use callback methods in your entities, or better yet an entity listener, to fill in audit information at various life cycle stages. In this case you could use listener with a @PreUpdate annotated method. Something like:
This is just a very minimal example to convey the basic idea. You could add much more audit data to the Auditable interface, and it would be a good idea to move the audit data fields out of the entity and into a seperate base class annotated as @MappedSuperclass. That sort of stuff.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.