-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Question about session.update( Object );
PostPosted: Mon Sep 15, 2003 1:41 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 8:41 am
Posts: 35
Location: Atlanta, GA
After making more extensive tests, I saw something that brought up a question...

I have an persistant object, say User.

User
userId
firstName
lastName
middleInit
userName
password

I wrote a small web app that changed only some of the properties of the object using struts. For argument's sake, lets say it was just changing the password. I have a struts form object that has a User object in it. When I submit the form, the password entered is automatically stored in User.password. The userId is also available.

So now all of the properties except userId and password are null.

When I do session.update( user ) it tries to update all of the fields in the db with what's in the object. What I would like it to do is only update the password.

Is there a setting or different method I should be doing to solve this problem besides doing ...

User user = session.load( User.class, userId );
user.setPassword( password );

I would like to try to avoid the extra step of having to query for what is already in the db and just update the properties that are set.

Thanx,

_________________
I am the reason spellcheck was created!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 1:51 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
I'm sure this is documented somewhere, but take a look at the DTD:
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd
<class dynamic-update="true">
Search here, as well, as Gavin has explained the possible performance implications. (It might not be faster)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 1:52 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
http://www.hibernate.org/hib_docs/refer ... pping-s1-3


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 1:55 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 8:41 am
Posts: 35
Location: Atlanta, GA
ahhh... thank you efender. I appreciate it!

_________________
I am the reason spellcheck was created!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 4:57 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 8:41 am
Posts: 35
Location: Atlanta, GA
Set the attriubutes that efender mentioned. It's still doing the same thing.

Am I missing something else?

In my Department.hbm.xml I have...

<class
name="com.cla.elms.persist.dao.department.Department"
table="DEPARTMENT"
dynamic-insert="true"
dynamic-update="true"
>

When I try to save, it still tries to edit all properties in the db. Have some that are non-nulls so I get this...

net.sf.hibernate.JDBCException: could not update: [com.cla.elms.persist.dao.department.Department#C01]: [IBM][CLI Driver][DB2/NT] SQL0407N Assignment of a NULL value to a NOT NULL column "TBSPACEID=2, TABLEID=4, COLNO=3" is not allowed. SQLSTATE=23502

Anyone? Anyone? Bueller?
=)

_________________
I am the reason spellcheck was created!!!


Top
 Profile  
 
 Post subject: Dynamic Update=true
PostPosted: Mon Sep 15, 2003 5:49 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:44 pm
Posts: 20
Look at this thread for more information why dynamic update doesnt work at certain times.
http://forum.hibernate.org/viewtopic.ph ... amicupdate

I have one question for experts.

If we have loaded the object in the session and if we change only one column in the object then Hibernate should only Update onbe column not all the columns in the object.This should happed without setting dynamic class properties.
Correct me if i am wrong.

Thx
Ashok


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2003 9:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
If we have loaded the object in the session and if we change only one column in the object then Hibernate should only Update onbe column not all the columns in the object.


You are quite wrong.

You need to set dynamic-update="true".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 9:11 am 
Beginner
Beginner

Joined: Thu Sep 11, 2003 8:41 am
Posts: 35
Location: Atlanta, GA
OK ... well dynamic-update is set to true and it is not working the way I would think.

Quote:
Search here, as well, as Gavin has explained the possible performance implications. (It might not be faster)


I can't see how the process of

1) submitting a form into persistant object
2) selecting from the db the persistant object based on id
3) setting all fields to that object
4) updating the database

could possibly be faster then

1) submitting a form into persistant object
2) updating the database

Even if Hibernate has to rebuild the update string.


Basically I'm trying to update on certian fields from a web form and not all the fields for the object are on the web form. So when I submit it, the fields on the persistant object that are not on the form will obviously be null. I would like to only update the items that have something in it. Is this possible?

_________________
I am the reason spellcheck was created!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 1:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummm. I think you are a bit confused about the available options. But no, we certainly do not do something crap like only update non-null fields. And we never will.

Now, when we use Session.update(), dynamic-update is not usually possible, because we don't know the previous state of the object in order to see "what changed". So we have a choice:

(1) just update all the fields
(2) select the current state and decide what needs updating

Thats your two choices.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 2:05 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 8:41 am
Posts: 35
Location: Atlanta, GA
Nope... not at all confused about my options...

Just stating that it would be a convenient and efficient option to also have. Hibernate may not know the state of the db, you are right, but what's to say that they application using Hibernate doesn't. I know I can think of a few places we could use it.

Just a suggestion.

_________________
I am the reason spellcheck was created!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 3:15 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
gavin wrote:
... we certainly do not do something crap like only update non-null fields. And we never will.

Hi guys, long time no see. :)

Gavin, one of my colleagues is having to do updates now, and he queried me about how he would update any field, in any object. I told him that he'd probably have to session.load the object, search through the incomming object (XML) and see what is NOT NULL and then set (update) each field, as per your post.

But then I started thinking. Wouldn't it be possible, and I am sure that lots of people would really like it, that a generic method be created (within Hibernate), perhaps like this:
Code:
public Object updateObject (Object inOjbect, Object databaseObject)

Both objects would have to be of the same type, which is mapped via Hibernate. Hibernate would then, using the mapping file, traverse all fields in each object and examine them doing the basic:

Code:
// For each field:
If ((inObject.getfieldX() != null) && (!inObject.getFieldX().toString().equals(databaseObject.getFieldX().toString())) {  // Obviously some form of Type casting needs to happen here... ?
    databaseObject.setFieldX(inObject.getFieldX());
}


The alternative to this, is basically to traverse every single field manually and check which is null and different...for every type of object you use...and this is a LOT of PT.

As I said, it'd be REALLY nice if Hibernate could do this. :) And yes, I know you said you never will...but...well, that was a while back.

-G


Top
 Profile  
 
 Post subject: Having same kind of problem
PostPosted: Fri Jan 16, 2004 4:51 am 
Newbie

Joined: Fri Jan 09, 2004 9:55 am
Posts: 10
Location: Rutland, UK
I'm having the same kind of problem i.e. updating an object when actually only changed a few things.

I've updated my whole object by getting the current state of the object and then overwriting the data in this with the bits that are in the form. End object is beautiful - everything I want it to be.

Then I try and pass it to the saveOrUpdate method but get the following error: 'Could not synchronize database state with session'.

I then commented out the code that updated the object using the form data and tried again - still the same error. Grrr.

Anyone have any ideas? Is it me? Do I need to sprinkle some Holy water on the server? Help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 4:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Brannor McThife wrote:
As I said, it'd be REALLY nice if Hibernate could do this. :) And yes, I know you said you never will...but...well, that was a while back.


You should be able to write something like that in an helper class pretty easily using the metadtata API for example.


Top
 Profile  
 
 Post subject: Re: Having same kind of problem
PostPosted: Fri Jan 16, 2004 4:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Horrigan wrote:
I'm having the same kind of problem i.e. updating an object when actually only changed a few things.

I've updated my whole object by getting the current state of the object and then overwriting the data in this with the bits that are in the form. End object is beautiful - everything I want it to be.

Then I try and pass it to the saveOrUpdate method but get the following error: 'Could not synchronize database state with session'.

I then commented out the code that updated the object using the form data and tried again - still the same error. Grrr.

Anyone have any ideas? Is it me? Do I need to sprinkle some Holy water on the server? Help!


Please open a new thread for totally unrelated questions like this. And give a more detailed description, like stack trace, mappings, code.


Top
 Profile  
 
 Post subject: Re: Having same kind of problem
PostPosted: Fri Jan 16, 2004 6:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Horrigan wrote:
Do I need to sprinkle some Holy water on the server?

Hum, Hibernate is exorcized once a week, should be fine ;-)

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.