-->
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.  [ 4 posts ] 
Author Message
 Post subject: how to deal with propagating IDs via webservice?
PostPosted: Mon Aug 22, 2005 8:14 pm 
Newbie

Joined: Wed Aug 10, 2005 5:03 am
Posts: 4
I have a client that is talking to a webservice that exposes a hibernate-based app, and I am unsure how to proceed about propagating id fields after they're set by session.save(persistentObj).


If client and hibernate were on same vm I could do the following:

Persistent newObj = new Persistent(); //id is null
hibersession.saveOrUpdate(newObj);

//now newObj's id is != null
//do some client-related work on same instance


But, if client and backend run on separate VMs and communicate via an axis web service we have:

//client side code
Persistent newObj = new Persistent(); //id is null
gui.displayWidgetFor(newObj);


//some time later
Persistent newInstanceOfObj = webservice.saveOrUpdate(newObj); //ws creates new instance but I need to keep using newObj

//so I try to simply copy field value
newObj.setId(newInstanceOfObj.getId());

//some time later
webservice.saveOrUpdate(newObj);

//but Hibernate complains:
Exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [common.TemplateLibrary#65536]|#]

What can I do?


Hibernate version:3.0.5

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 8:29 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I think you need to use a merge in this case.

for last step...to update some time later... do
Code:
session.merge(newObj)

instead of
Code:
saveOrUpdate()


from hibernate docs...
Quote:
Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.


Top
 Profile  
 
 Post subject: what about first time save?
PostPosted: Mon Aug 22, 2005 9:00 pm 
Newbie

Joined: Wed Aug 10, 2005 5:03 am
Posts: 4
I currently save/update using session.saveUpdate(). Your suggestion makes sense, but seems like merge() is strictly an insert. what if the subroutine has to be robust enough to handle both new saves and merges? should I change to?:

public saveMyOBject(Persistent obj)
{

if(session.contains(obj))
{
session.merge(obj);
}
else
{
session.save(obj);
}


thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 9:02 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
Actually from the API docs for merge...

Quote:
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".


I haven't tried it but, I'd say it handles it for you. Hibernate rocks!


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