wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes SaveOrUpdate API - Hibernate BUG? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "SaveOrUpdate API - Hibernate BUG?" Watch "SaveOrUpdate API - Hibernate BUG?" New topic
Author

SaveOrUpdate API - Hibernate BUG?

Rvrk reddy
Greenhorn

Joined: Oct 16, 2007
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rvrk reddy
Greenhorn

Joined: Oct 16, 2007
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Oct 16, 2007
Posts: 10
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

Joined: Oct 16, 2007
Posts: 10
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

Joined: Jan 08, 2012
Posts: 2
Hello,

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

Regards,
Pranav C Lunavat
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

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.


[How To Ask Questions][Read before you PM me]
 
Don't get me started about those stupid light bulbs.
 
subject: SaveOrUpdate API - Hibernate BUG?
 
Similar Threads
Rereshing after modification / Listing
Performance
Lost update problem and web apps
GUI display & booking questions
@Embedded not loading with owner entity