-->
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.  [ 3 posts ] 
Author Message
 Post subject: Updating a partially filled object
PostPosted: Thu Dec 06, 2007 9:27 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Hibernate version:3.2.4
Name and version of the database you are using: Oracle 10g

I have read through the documentation and I have searched google and the groups, but I am not able to come up with a satisfactory solution... Part of the problem is that i don't know how to phrase the question for a search engine.

Here is a little detail about my project:
I have a set of interfaces that were built to abstract away the back end, allowing projects to develop software (primarily web-based) that would allow them to not be concerned with the system used on the back (there are multiple different commercial off the shelf (COTS) products that can be used on the back to satisfy the needs of the front) - sorry if that is cryptic, i have to be.

The initial implementation used a COTS product to insert/update the data and used plain old JDBC to retrieve the data. Additionally, we have some items that are externalized from the COTS product, which use JDBC for everything. I want to convert our retrieval of COTS data and the insert/update/delete of externalized data to hibernate.

Here is the problem... We currently support the concept of lazy update. The front-end will often create the object from scratch using the primary key data and then set only the fields it wishes to change. So, if i have an object with 1 pk and 5 fields, it is possible that only 2 will be set and then update will be called. This means that only the 1 changed field (the one that is set) needs to be updated and the other 4 will remain the same.

Is there a mechanism in hibernate that currently supports this? What about an easy way to merge that object with an object obtained from hibernate (i looked at the merge function, but that appears to only bring that object back into the session and I believe that when update is called, it will null the other 4 fields).

I appreciate any assistance.

Thanks,

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 1:07 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi Paul,

As you want to update only particular rows, you will have to use HQL and it is will have lot of coding.

The code below is not test, just a way:
Customer cust = // You will get it from your front end.
String hql = "update Customer set name = :newName, desc= :newDesc where id = :oldId";
Query query = s.createQuery( hqlUpdate )
if(cust.getName() !=null)
query.setString( "newName", cust.getName() )
if(cust.getDescription() !=null)
query.setString( "newDesc", cust.getDescription() )

query.setString( "oldId", cust.getId() )
int updatedEntities = query.executeUpdate();

Look into the below link for updating specific columns.
http://www.hibernate.org/hib_docs/v3/re ... tch-direct
Note: dont use one with alais as stated in documentation.

But for doing it like this, may be you can get the object back from the db and then update the object with the new values and using session.update() you can do it.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 4:01 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Hi Ramnath,

Thanks for that... I didn't really realize I could use HQL to do updates. I think that this will make a great alternative when (if) i am having scalability issues with my first approach. Since we have internal flags built into all of our interface implementations, I know exactly what fields have been set, so I can use those flags to direct a reflection based merge in which I get the object from hibernate, then update the fields that have changed - basically your last suggestion. I have been tossing up this idea, but wanted to see if there was another, more direct solution provided by hibernate. I am always weary of using too much reflection as i am concerned with scalability (although java has come a long way in the last few releases).

Thanks for your help.

Paul


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.