-->
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.  [ 9 posts ] 
Author Message
 Post subject: save() or saveOrUpdate()
PostPosted: Thu Mar 15, 2007 11:11 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 10:19 am
Posts: 24
if I always use saveOrUpdate() is it ok? Hibernate will understand on its own that an object is new so it will persist it in the database
or an object exists in the database and it will updated?Does saveOrUpdate() covers both cases?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 11:29 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
It is OK to use saveOrUpdate() exclusively as long as the unsaved-value of the primary key is set properly (default is usually sufficient).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 12:04 pm 
Beginner
Beginner

Joined: Wed Mar 14, 2007 10:19 am
Posts: 24
Ananasi wrote:
It is OK to use saveOrUpdate() exclusively as long as the unsaved-value of the primary key is set properly (default is usually sufficient).



Can you please give me an example?You mean in the mapping?

What about if I use save() and this object is already in the database.Will I have a exception?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 12:14 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
OK, here goes.

Code:
<hibernate-mapping>
  <class name="eg.Person" table="Person">
    <id name="id" type="java.lang.Long" unsaved-value="null"/>
    <property name="name" type="string"/>
  </class>
</hibernate-mapping>


The unsaved-value tells Hibernate that if the id is null, then the object should be considered transient (a new object) and that saveOrUpdate should save the new object via INSERT. If there is a value in the id property, then Hibernate should consider the object as persistent or detached (an object that already exists in the database) and that saveOrUpdate should save the object via UPDATE.

For most cases, the unsaved-value property default should suffice (the default unsaved-value for any Object is null, for primitives, it will be 0), and does not need to be specified in the mapping document explicitly.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 12:16 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
And yes, if the object already exists in the database and you call save() it will throw an exception.


Top
 Profile  
 
 Post subject: Re: save() or saveOrUpdate()
PostPosted: Thu Mar 15, 2007 8:53 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
juana wrote:
if I always use saveOrUpdate() is it ok? Hibernate will understand on its own that an object is new so it will persist it in the database
or an object exists in the database and it will updated?Does saveOrUpdate() covers both cases?


You are doing the rite thing .. the only case when you would want to use save is when you are expecting the object to be new ( you are expecting an exception when its already there )

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 4:11 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 10:19 am
Posts: 24
so I should use the saveOrUpdate() right?Since the default is unsaved-value=null ?I mean I do not have to change anything else to use in both cases saveOrUpdate().


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 8:47 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Correct.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 9:28 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 10:19 am
Posts: 24
Thank you very much


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