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.  [ 9 posts ] 
Author Message
 Post subject: why many-to-one only performance insert but not update ?
PostPosted: Fri Dec 12, 2008 11:26 pm 
Beginner
Beginner

Joined: Mon Dec 17, 2007 11:08 pm
Posts: 47
hi, I had a class like this

Code:
public class User {
private Address address;
  //setter and getter..
}

<many-to-one name="address"
           column="address_id"
           unique="true"
           not-null="false" cascade="all"/>



my question is when i call session.save(user), it perform SQL insert to a new row into address table, but not SQL update ? why ?

how can i make it to perform SQL update the row instead of perform SQL insert ?

any idea ?

kiwi

_________________
happy hacking !


Top
 Profile  
 
 Post subject: Re: why many-to-one only performance insert but not update ?
PostPosted: Mon Dec 15, 2008 3:47 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
kiwionly wrote:
hi, I had a class like this

Code:
public class User {
private Address address;
  //setter and getter..
}

<many-to-one name="address"
           column="address_id"
           unique="true"
           not-null="false" cascade="all"/>



my question is when i call session.save(user), it perform SQL insert to a new row into address table, but not SQL update ? why ?

how can i make it to perform SQL update the row instead of perform SQL insert ?

any idea ?

kiwi


Please post your code, how you save your user. You should retrieve the address exists in database, and set it to user instead of creating the new address.


Code:
Address addr = ... // load from the database
User user = new User();
...
user.setAddress(addr);
session.save(user);




Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 4:11 am 
Beginner
Beginner

Joined: Mon Dec 17, 2007 11:08 pm
Posts: 47
hi, my code is something like this..

Code:
Address add = user.getAddress();

add.setPostcode("some value");

user.setAddress(add);
session.save(user);


the problem is it create a new one instead of update the current one.

any idea ?

kiwi

_________________
happy hacking !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 4:35 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
kiwionly wrote:
hi, my code is something like this..

Code:
Address add = user.getAddress();

add.setPostcode("some value");

user.setAddress(add);
session.save(user);


the problem is it create a new one instead of update the current one.

any idea ?

kiwi



Why do you not use :
Code:
Address add = user.getAddress();

add.setPostcode("some value");

session.update(add);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 4:49 am 
Beginner
Beginner

Joined: Mon Dec 17, 2007 11:08 pm
Posts: 47
hi, i had try that, but it just not work!

i doubt is related to the relationship i define in my hbm.

Code:
<many-to-one name="address"
           column="address_id"
           unique="true"
           not-null="false" cascade="all"/>


_________________
happy hacking !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:01 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
kiwionly wrote:
hi, i had try that, but it just not work!

i doubt is related to the relationship i define in my hbm.

Code:
<many-to-one name="address"
           column="address_id"
           unique="true"
           not-null="false" cascade="all"/>



I have tried your code. No problem with your configuration


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:26 am 
Beginner
Beginner

Joined: Mon Dec 17, 2007 11:08 pm
Posts: 47
hi, are u mean it performance an update instead of insert ?

I use it with spring MVC. in fact all my code work accept this one, i still need to debug on this.

_________________
happy hacking !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:30 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
kiwionly wrote:
hi, are u mean it performance an update instead of insert ?

I use it with spring MVC. in fact all my code work accept this one, i still need to debug on this.


yes, it performance update


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 12:19 pm 
Beginner
Beginner

Joined: Mon Dec 17, 2007 11:08 pm
Posts: 47
hi, i had find out the problem.

u r right.. actually I not reuse the detach object. (i had solve it by reattach the object retrieve from database and put into spring mvc @controller 's @session attributes).

since the new object with session.merge(user) will insert a new object into database (because the new object id is null).

thx.

kiwi

_________________
happy hacking !


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