We use Hibernate integrated in a Struts2 web application. If we want to perform an update to our objects on the database we fetch the data and load the resulting object into a web form where the user can edit the data. In this web form not all the objects attributes are displayed and editable. Submitting the form data will create a new object with the values set in the web form, which is then saved to the database again using Hibernate.
Since not all the attributes from this certain object were set in the web form, some values result as null, which leads us to the problem that these values (even though there are set in the DB) get overwritten with null values. Hibernate is set to dynamic-update=true in all involved mappings.
There are (at least) three workarounds that we could think of:
1. Adding the missing attributes as hidden fields in the web form (not very flexible)
2. Adding the missing attributes in the Java code before saving the object to the DB (not very flexible either)
3. Writing a utility class which merges the data from the persistent object with the one created by the web form (flexible but cumbersome).
What we do right now is to iterate through all methods, find out the getterMethods and setterMethods of both the "old" database-object and the "new" Hibernate object, compare values and take the values which are not null. This is not a very satisfying resolution, because some objects contain Other objects with values and so on, sometimes three or more layers of depth. Also, using reflection in Java is not a very nice resolution.
We are sure there must be other people out there facing similar problems but even after quite some research efforts we were not be able to find anything of help in the web.
We are even not sure if this is more a Hibernate or a Struts2 problem.
We'd be really grateful if someone could point us in the right direction. (Or Thread)
Thanks in advance!
Nicolas & Alex
Edit: We also posted this question in a struts-related forum here:
http://www.coderanch.com/t/509027/Struts/Struts-Dynamic-Merge-two-webform because, as said, We are even not sure if this is more a Hibernate or a Struts2 problem. We're also on the struts user-mailing-list and I will post any resolutions form there in both forums.