-->
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: How to reset a value of a attribute(column) of a row?
PostPosted: Tue Feb 06, 2007 10:40 pm 
Newbie

Joined: Thu May 20, 2004 9:15 pm
Posts: 4
Hi
I have a basic question(being newbie to hibernate)
I use hibernate to load Objects from Database. Once I load the object with let's say 10 attributes, how do I reset the value of few of those attributes?
If I call the setter method and pass null, that works but when I commit, the actual values in the database are not getting reset.
Is there a flag I should set for these kind of situations?

Thx
Ravi


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 11:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
What do you mean by reset? Changed to some default value, or changed to what they were before some recent in-memory updates?

Hibernate doesn't provide a way to do either of these things. You have to know the default values yourself, and you have to manually undo changes or rollback transactions if you need to.

You can use session.refresh(object) to reject all changes in object (i.e. get the current DB values back into the object), then re-make all the "correct" changes. But the best plan is to only make "correct" changes in the first place. One way of doing this is to construct an in-memory version of the relevant object and make your changes to it. When you're ready to save, copy the "correct" changes to the persistent version of the object, and save that.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 3:15 am 
Newbie

Joined: Thu May 20, 2004 9:15 pm
Posts: 4
Hi
Let's say I have a Object X(and table) with following attributes
Integer A (Primary Key)
Integer B
String C
String D
....

Let's say that all the values are populated and not null.
I load this object from database using Hibernate in a Tx. Make changes so that C is set to null and D is set to null. When I commit the Tx, I expect Hibernate to reset these values in the database to be null(for that particular row). That doesn't seem to happen. But if I change the values of C and D to different value, those get updated just fine in the database.
How do I reset values for Attributes C and D since setting them to null doesn't seem to work.

Thx
Ravi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 4:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Setting them to null certainly works in most cases. The only way I can think of to get that to happen is to set dynamic-insert="true" and/or dynamic-update="true" on your class mapping. If you've done that, remove it.

_________________
Code tags are your friend. Know them and use them.


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.