I'm using Hibernate as my persistence framework for a project. I'd like to start a discussion on best practices for performing updates.
Specifically, my problem is with retrieving data from html forms and then updating only those values that were modified by the user. For example, my Product update form (I'm using Struts BTW) may only display 5 of the 10 Product attributes. When the user submits the form, I can't just update the object in Hibernate because only 5 of the 10 attributes have values.
I need some way to merge the updated attributes with the attributes that exist in the database.
Here are the ideas I've had:
1) when updating in Hibernate, first reload the persistent version of the object, apply the changes from the web form and update
2) Store the original objects as retrieved from Hibernate in the session. After the web user submits the form, update the original objects in the session with the values from the web form.
3) send all 10 user object attributes to the form and hide the ones that shouldn't be updated using hidden inputs. When the form is submitted, all attributes will be present so that I can just update using Hibernate. (I realize there is danger here by allowing the web user to see things that perhaps they shouldn't)
I would go for 1) because you should keep your session small to make your application scalable and to have the option to cluster. Second, messing around with hidden fields you do not need is error prone. You may forget one.
Just use the setter for fields you can input that's it. You might consider not to use your mapped class inside your formbean in this case but explicite fields.
Best Regards Sebastian
Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="http://www.laliluna.de" target="_blank" rel="nofollow">www.laliluna.de</a>
Joined: May 27, 2004
Thanks for your input.
I actually tried #3 on a few forms and it turned out OK but only because I was allowing the user to update almost all of the attributes for a particular object.
However, in cases where the user is allowed to update only a few attributes, I will use approach #1 as you suggest.