Hello,
Yes, I'll share with you possible solutions that I found. Below is the two best ways that I found to accomplish this:
1.) The efficient way.
The most efficeint way is to check which fields (from your web tier) are not null and then obtain a straight JDBC connection from your Hiberate Session object and insert only those non null values.
2.) The easy way.
An easy way to do it (and the way that I choose) was to implement a method named
merge in the Hibernate pojo that I wanted to update with non null values. A call to the method looked like:
Code:
pojo1.merge(pojo2);
You can pretty much guess what it does... The merge method looks at all property values in pojo2 and for values that are NON-NULL then calls the respective setter method on pojo1, passing in pojo2's value, like:
Code:
if(pojo2.getProperty1() != null) pojo1.setProperty1(pojo2.getProperty1());
Here the pojo1 object is the object created and populated by your web/business tier. pojo2, is the object brought back from a Hibernate session.get(id); (Note that you must store the id of the pojo in the html form, so that you know which pojo you want to merge). This works because hibernate loads all of the original data in the pojo, and then your merge method fills in ONLY the new (non-null) values. There is no longer a need to call
update, since hibernate will see the values have changed and do an auto commit.
Implementing the merge method can be tedious at first, but once completed it can be a very powerful thing. I have one method in my DAO that I know use to "update" the pojo from any given web form. The method signature is simple:
Code:
...
public void configureItem(Long itemId, Item newItem)
{
Item persistedItem = (Item) hibernateSession.get(itemId);
persistedItem.merge(newItem);
}
.....
Hope it helps and good luck! :)