-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Update non null values only? Is there a way....
PostPosted: Fri Apr 29, 2005 5:35 pm 
Newbie

Joined: Thu Jan 20, 2005 3:02 pm
Posts: 13
Hibernate version: 2.1.8

Name and version of the database you are using: MySQL 4.1

Main Question:

Is there a way to have hibernate update ONLY non-null values of a Detached object?

Scenerio:

I have web based 3 tier system, where Hibernate persisted POJOs are read, and passed to the web layer and used to render the HTML of the page.

I store the POJO ids in hidden form fields, and on submition, I recreate the object in the web tier (and assign it its respective id).

Some of the properties of this hibernate POJO are represented as form fields, those are also placed in the recreated POJO. The user can change the values (or leave them be) and the values change accordingly with a call to:

Code:
session.update(pojo);


However, those hibernate properties that are not saved in the HTML form, and therefore are not in the recreated object, have null values, and when this recreated POJO hits the session.update(pojo) method, the old values (which are desired remain unchanged) are nulled out.

Am I missing some easy way to only update non nulls with hibernate?


Top
 Profile  
 
 Post subject: How did you resolve this?
PostPosted: Thu May 12, 2005 11:46 pm 
Newbie

Joined: Thu May 12, 2005 11:42 pm
Posts: 4
Location: Perth, Australia
Hi I was wondering how you resolved this issue


Top
 Profile  
 
 Post subject: Solutions
PostPosted: Fri May 13, 2005 8:28 am 
Newbie

Joined: Thu Jan 20, 2005 3:02 pm
Posts: 13
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! :)


Top
 Profile  
 
 Post subject: Correction
PostPosted: Fri May 13, 2005 8:34 am 
Newbie

Joined: Thu Jan 20, 2005 3:02 pm
Posts: 13
Whoops,

Looking over my code.....

A very minor correction:

Code:
hibernateSession.get(itemId);


should be:

Code:
hibernateSession.get(Item.class, itemId);


:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.