-->
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.  [ 11 posts ] 
Author Message
 Post subject: Partial update
PostPosted: Wed Feb 04, 2009 5:22 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
Hibernate version: 3.2.6.ga

Is there a way apart from native SQL to persist partial object (i.e. just updating the fields with values in the object and not affecting other fields with null values)

for eg:
I have personInfo with below fields
username (this is the id/pk, I will always have this value in the object)
password
firstname
lastname
age
sex...so on

I want to update only 2-3 fields of this table. So can i use saveOrUpdate passing the partially populated personInfo object leaving all the other properties null in the object and unchanged in the db?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:26 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
If you want to update only those fields, which have changed, you could use <class ... dynamic-update="true">

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:31 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
I have added that and I get this error:

org.hibernate.exception.ConstraintViolationException: could not update: password

org.hibernate.util.JDBCExceptionReporter - Cannot insert the value NULL into column 'password', table 'person'; column does not allow nulls. UPDATE fails.

Any more suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:42 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
How do you change your object? I guess, you are creating a new Object and populate just the fields you want to be changed. That is not going to work, as you need to fetch your entity, change the loaded one and then flush the session, so that hibernate is able to detect changed fields.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:47 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
In case you are somehow not able to work on a loaded object, but still insist on only updating not-null fields:
You could create your own update-method, which checks for all non-null fields on a given object and then changes loaded one, e.g.
Code:
public void updatePartially(PersonInfo info)
{
  //get a session and start a transaction ...

  PersonInfo old = session.load(PersonInfo.class, info.getUserName());
 
  //look for not-nulls
  if(info.getFirstname() != null) old.setFirstname(info.getFirstname());

  //do that for all fields ...

  session.flush();
}


Rating is welcome. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:52 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
I get my object from Hibernate, pass it to my UI layer, and by the time it comes back it has changed completely and Hibernate knows nothing about it I suppose.
So I need to load the object from the db using my id, then how do I save the object with the values in my Object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:55 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
oops, posted that before seeing your reply. Thanks very much


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 6:00 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
mcbi4kh2 wrote:
I get my object from Hibernate, pass it to my UI layer, and by the time it comes back it has changed completely and Hibernate knows nothing about it I suppose.


In that case use session.merge(), which reattaches your detached object to the new session.

Please rate me, if it helped you.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 6:22 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
If I use merge, Hibernate complains about firstname being null, it is null in my object but not in the db.

org.hibernate.util.JDBCExceptionReporter - Cannot insert the value NULL into column 'firstname'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 6:52 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Why is it null? I thought you load an object, pass it to the GUI-Layer, edit it and then pass it back. In that case you should not loose any information. What about the own updatePartially-method, mentioned above?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 7:12 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
That person thing is just something I made up for concisesness, I have about 45 different fields in my actual table. Some of them I dont need and so dont really want to pass them around my application.
I figured so long as I keep my Id then I would be able to update the non-null values.

Looks like thats not possible so I'll try and write a more generic method of what you mentioned earlier.
Many Thanks


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