-->
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.  [ 7 posts ] 
Author Message
 Post subject: save and saveOrUpdate problem
PostPosted: Tue Dec 16, 2003 8:51 am 
Regular
Regular

Joined: Thu Nov 20, 2003 10:44 am
Posts: 58
Location: Paris, France
Dear all,
I don't understand how to manage save and update.
Actually i would like to save value when there is no data for the key and update when any.

I use saveOrUpdate function but when there is not data, it crashes and if I use save function it crashes when there is data !

Moreover I use a string key and unsaved-value="null" for this key so it is strange ....

My mapping is as follows :
Code:
<hibernate-mapping>
   <class name="fr.gouv.finances.dgi.opale.commun.transverse.persistent.SirenPersistent" table="SIREN">
      <id name="numSiren" column="PK_NUM_SIREN" unsaved-value="null">
         <generator class="assigned"/>
      </id>
      <property name="entrepriseDge" column="APPARTENANCE_DGE" not-null="true"/>
   </class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 9:46 am 
Regular
Regular

Joined: Thu Nov 20, 2003 10:44 am
Posts: 58
Location: Paris, France
Nobody has an idea ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 10:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is a FAQ.
http://www.hibernate.org/116.html#A11
and somewhere in the doc.

If using hibernate 2.1, you can use the <version unsaved-value="...">

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 10:35 am 
Regular
Regular

Joined: Thu Nov 20, 2003 10:44 am
Posts: 58
Location: Paris, France
I agree but as I put unsaved-value="null" .. I do not understand why it is not possible to create a new row with saveOrUpdate when there is no data !!!

HELP


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 10:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You are using an assigned generator. Have a look at section 9.4. of the reference guide.

With Hibernate, you can't save an object with a null id in DB. This is not allowed and this would inconsistent with DB constraints.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 11:16 am 
Regular
Regular

Joined: Thu Nov 20, 2003 10:44 am
Posts: 58
Location: Paris, France
Ok so is there a way to check if the data are in the DB ? (except doing a select)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 11:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes, several solutions :
- business rules allow you to know if it's a new object, then explicitly call save() or update() : not that good.
- business rules allow you to know if it's a new object, thus implement it in an interceptor isUnsaved(). Have a look at section 9.4
- use version or timestamp in your object, thus you can use a version unsaved-value. If the object has been saved, then version is not null, and Hibernate can know it. Note this is a 2.1 feature.

_________________
Emmanuel


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