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 object’s 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 alle 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.
Thanks in advance!
I've almost always solved this issue with a DTO that I use with a BeanUtils-style property copier (deep or shallow, depending). One reason is that this way the data exposed to/received from the view layer is exactly what's necessary, which can save headaches later on. It *is* a pain, though.
Joined: Sep 03, 2010
Finally we discovered several solutions for this problem. I will list them here for future users.
First of all, what we finally did and worked for us:
in the affected class, we implemented preparable like this:
and added an override like this:
which then initialized the Object as desired and directly worked with the database Object. More Information: Click here
Use BeanUtils.CopyProperties for simply gaining and setting all the properties: Link here
Just another solution, which has the same concept, but is more flexible, is to write a wrapper like this one:
Put @InOut annotation on your model with scope set as session. This outjects the object loaded from DB into session just before forwarding to the JSP. On submission, the object is injected back into the action class from session before the params interceptor is invoked, which will update the same object from http request. So, basically its the same object that gets updated.