Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SaveOrUpdate API - Hibernate BUG?

 
Rvrk reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m trying to update a object with one-one association, some thing like:
//User has one-one relation with Address, WebAddress is subclass of Address.
----------------------
//retrieve first
User user = bean.retrieveUser(id);

//update
user.setName(�Jon�);
WebAddress address = (WebAddress)user.getAddress();

address.setEmail(jonlL@citi.fin.com);
user.setAddress(address);
bean.updateUser(user);
-------------------------------
Update calls saveORUpdate API.
Retrieve works fine. On update (WebAddress table) it�s trying to create a new record instead of updating. UserID is PK in all 3 tables (USER, ADDRESS, WEBADDRESS).
Obviously INSERT fails because there is already record in database.
Some where ID�s/associations are disconnected.
On session.refresh(user.getAddress()) --> exception is "The class has no identifier property".I would highly appreciate inputs.

Thanks in advance, R.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not a Hibernate bug. If you want to find out if something is a bug in Hibernate, they have a JIRA Issue tracking site you can use.

When you call saveOrUpdate you will either update an existing record or insert a new one based on how Hibernate checks unsaved values - normally, whether or not the id been changed, or is not set. What is the Object references by bean? Does it actually load the WebAddress from the session?
 
Rvrk reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
No, not a Hibernate bug. If you want to find out if something is a bug in Hibernate, they have a JIRA Issue tracking site you can use.

When you call saveOrUpdate you will either update an existing record or insert a new one based on how Hibernate checks unsaved values - normally, whether or not the id been changed, or is not set. What is the Object references by bean? Does it actually load the WebAddress from the session?



I�m retrieving User details in client through Service (Bean), update User address, and call different sevice API (updateUser) to update.
When �user� object reaches Bean, some how ID�s are disconnected.
Do I need to reload Address before invoking saveOr Update?
On session.load(User, userId)  exception is nonunique identifier��
Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When �user� object reaches Bean, some how ID�s are disconnected.

Are you using the same Session between both methods? If you you will have to either reload you POJO or reattach it to a Session, otherwise Hibernate thinks you want to create a new object.


On session.load(User, userId)  exception is nonunique identifier�

Is your userID really a primary key? Or is it possibly null?
 
Rvrk reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:

Is your userID really a primary key? Or is it possibly null?


Thanks Paul. Yes, I'm using same session (always current session).
It works for all other update graphs(collections), this use case is slightly different. We have same User.Id as PrimaryKey (Address.userId is PK & FK, WebAddress � userId is PK&FK), basically same ID identifies all (because it�s 1-1 relation).

Mapping files:

Mapping file looks like:
*************
Address.xml:

***********************
WebAddress.xml:

I�m really stuck with this problem; I would appreciate your help!

Note: Same code works if I do every thing in a single service method (retrieve, update, commit in the same current session & API). Not sure why it's disconnected on passing to client.


Thanks, R.

[ Edited to use code tags - Paul Sturrock ]

[ October 16, 2007: Message edited by: Paul Sturrock ]

[ October 16, 2007: Message edited by: Rvrk reddy ]
[ October 16, 2007: Message edited by: Rvrk reddy ]
 
Rvrk reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rvrk reddy:
[QB]

Thanks Paul. Yes, I'm using same session (always current session).
It works for all other update graphs(collections), this use case is slightly different. We have same User.Id as PrimaryKey (Address.userId is PK & FK, WebAddress � userId is PK&FK), basically same ID identifies all (because it�s 1-1 relation).

Mapping files:

Mapping file looks like:
*************
Address.xml:

***********************
WebAddress.xml:

I�m really stuck with this problem; I would appreciate your help!

Note: Same code works if I do every thing in a single service method (retrieve, update, commit in the same current session & API). Not sure why it's disconnected on passing to client.


Thanks, R.

[ Edited to use code tags - Paul Sturrock ]

[ October 16, 2007: Message edited by: Paul Sturrock ]

[ October 16, 2007: Message edited by: Rvrk reddy ]

Paul:
I reviewed documentation, but re-attachment is not really working.
I'm getting exceptions like "there is another object with same identifier".
Only thing works:
delete old object, recreate from client input. This is ugly but it works.
I�m doing something like {create new session, load User, delete Address Association (WebAddress ), flush & close this session} , { invoke saveOrUpdate(User) in current active session.

Can you please help me out. I would really appreciate your valuable inputs.
Thanks, R.
 
Pranav Lunavat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am also facing same issue.
Can somebody help if they know the solution

Regards,
Pranav C Lunavat
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a really old thread I would encourage you to start your own topic with more details if you need more assistance. You can try using merge.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic