-->
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.  [ 5 posts ] 
Author Message
 Post subject: Assigned ID problem
PostPosted: Fri Feb 20, 2004 10:57 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Hi,

I have searched the forum extensively but could not find an answer for my question.

I have data coming from an XML file which I want to persist. I can use assigned ID's (these come from the data in the XML), but the default behaviour I experience from Hibernate with assigned ID's is that it trys to
perform an update. The problem is, that this might not be want I need, because it could also be possible (obviously ;) that the data in XML file is new.

How can I tell Hibernate to perform an insert insteed of an update in these cases?

Sure, I could do this "by hand", but maybe there is build in solution in Hibernate.

Thanks for your help.

Best regards,

juergen


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 11:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There are three possibilitys how Hibernate can distinguish automatically if it should do an update or a save:
  • you use unsaved-value for your id
  • you use unsaved-value for your version property
  • you use an interceptor with the isUnsaved method implemented

I don't see any problem with assigned and unsaved-value however (unless you really have no way yourself to find out what is unsaved and what not)


Top
 Profile  
 
 Post subject: Assigned ID problem
PostPosted: Fri Feb 20, 2004 11:10 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
What I was trying to do is.

Code:
myObject.setId( id );  //id from XML
session.saveOrUpdate( myObject );


Can I tell Hibernate in some way that if the update fails, automatically
perform an insert?

Thanks,

juergen


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 11:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No, you have to do this manually. Or you could use saveOrUpdateCopy() which will try a SELECT before the UPDATE.


Top
 Profile  
 
 Post subject: Assigned ID problem
PostPosted: Fri Feb 20, 2004 11:23 am 
Newbie

Joined: Mon Jan 05, 2004 10:46 am
Posts: 13
Ok, I understand.

Thanks for help.

Best regards,

juergen


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