-->
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.  [ 7 posts ] 
Author Message
 Post subject: I just want to update 1 column, without reading the row
PostPosted: Thu Feb 17, 2005 6:10 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
Hello,

I'm new to hibernate (obviously from this question :-). I have a scenario where I have to update a column in a row in the database.

Presently what I have to do is load() the object, change the field, then saveOrUpdate() the POJO.

It strikes me as iefficient to read the object first. So I tried creating an instance of the POJO, assigning the ID field correctly, setting the value of the column-to-change, and calling session.update().

The transaction failed stating that I had non-nullable fields defied. Of course I can't have those fields NULL in the database, but I thought perhaps I could keep the null in the POJO, and have hibernate update that column without having to load() the object first.

Any thoughts?

Thanks,

Mal.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 6:16 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
Also, I thought hibernate was supposed to only update() the columns that changed, not all of them?

I am loading() the record in Hibernate, then modifying one field (the password, then calling saveOrUpdate().

When looking at the hibernate SQL logging, I see something like...

Hibernate: select a, b, c, d, e, f, g
Hibernate: update users set b, c, d, e, f, g

... even though I'm only changing field E for example.

Any thoughts?

Mal.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
dynamic-update=true


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 6:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Presently what I have to do is load() the object, change the field, then saveOrUpdate() the POJO.


You do NOT need to call saveOrUpdate() here!

Quote:
It strikes me as iefficient to read the object first. So I tried creating an instance of the POJO, assigning the ID field correctly, setting the value of the column-to-change, and calling session.update().


Don't do this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 7:10 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
Michael,

Setting dynamic-update did in fact solve the second problem... Only the necessary columns are being updated.

However, my first problem (having to load() and object before being able to update it) has not been solved.

Gavin seems to think (correctly) that my "keep POJO values null except those I want to update" method is not good. I promptly ended up with null fields in my database :-) Woops.

Is is possible for me to update the user's password without first having to load the entire user object into a POJO?

Perhaps this is one of the db versus java-persistence issues I have to get used to?

Thanks,

Greg.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 7:12 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
gavin wrote:
Quote:
Presently what I have to do is load() the object, change the field, then saveOrUpdate() the POJO.


You do NOT need to call saveOrUpdate() here!



Gavin,

I'm not quite sure how the object will get persisted if I don't call that method, or call update() instead... What do you propose I do instead? Simply modifying the POJO value won't get the object stored in the DB (as far as I know?).

Thanks,

Malachii.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2005 7:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, thats exactly what happens.

Please read the documentation.


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