-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help with session.saveOrUpdate()
PostPosted: Mon Apr 30, 2007 11:00 am 
Newbie

Joined: Mon Apr 30, 2007 10:57 am
Posts: 2
Hi,

I am having problem with saveOrUpdate. (ONly when doing an Update)

I create a database object.
Account a = new Account();
where Account is a table with 10 columns, out of which 2 keys form the composite primary key.

I set values for 5 columns like...
(assume Name and ID form composite Keys)
a.setName("aaa");
a.setId("12");
......

after that I call the method session.saveOrUpdate(a);

I know that my database has a record with the above composite key....
so..I just want to update the remaining 3 rows for which i have set new values. I want the reamining 5 columns with the values that already existed.

But what is happening is the 5 colums(with the new values) that i set are seen in the database, but the values for the reamining 5 columns are set to 'null' in the database coz these columns also got updated(since i did not set any values for these, the default values null was set).

I want to avoid this and I want to update only columns that i set the values for??

Can anyone help me regarding this...!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 11:13 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
To do an update you must first get the Object from the DB using a hibernate session or DAO, change your values and then do a save...

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 12:47 pm 
Newbie

Joined: Mon Apr 30, 2007 10:57 am
Posts: 2
If i get the object from DB then i can directly call session.update().

My case is

I want to create an object.
Insert that object in DB if that row does not exist.
If is exists then update the row with the new modified values.

thats y i am using saveOrUpdate() method


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 5:16 pm 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
As I said previously you can only update an object if you get it from the database first...
How about implimenting your own saveUpdate method that calls a checkIfExists method, if the object exists it gets it from the DB, sets it with your new params and saves it; if not it can simply save the new object...
Hibernate can't read your mind.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 8:59 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Did you try merge() ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 5:55 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
If I did understand correctly what you want to do is set
dynamic-update="true"
on the class-element in your mapping-file.
Then hibernate will update only those columns that do not contain null-values.


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