-->
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.  [ 10 posts ] 
Author Message
 Post subject: session.save works but session.saveOrUpdate doesnt work
PostPosted: Sun Jul 25, 2004 12:06 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
Hi,

I have Hibernate 2.1.4. I have a class with an int primary key, which is "assigned" type. When I create a new instance and "session.save()" to the DB, it works fine (creates an insert SQL). However, when I change "save()" to "saveOrUpdate()", I get an exception:

net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:689)

I would think on saveOrUpdate, Hibernate would do a select, see if the key is in the DB, if not, do an insert, if so, do an update. When I sniff the SQL it is just doing an update with no select.

Is this how Hibernate is supposed to work (especially with assigned id's), or am I doing something wrong, or is this potentially a bug?

Thanks,
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 25, 2004 12:26 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Do you have an unsaved-value specified in your mapping for the Id?

For example,


<id name="id" column="ID" type="java.lang.Long" unsaved-value="null">


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 25, 2004 12:41 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
gpani wrote:
Do you have an unsaved-value specified in your mapping for the Id?


No I didnt. I need to get used to the mindset that the primary key should not be a piece of business data. e.g. on my person table my primary key should be a sequence and not the ID of the user that we use. I will add a "native" primary key column, and not an "assigned" one, and we should be fine. Thanks! Chris

ps. If anyone else has another idea, let me know. For "assigned" keys, I would think this is a bug, but since I am new to Hibernate and this seems like a simple thing, I guess it is not.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 25, 2004 1:09 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
mchyzer wrote:
For "assigned" keys, I would think this is a bug, but since I am new to Hibernate and this seems like a simple thing, I guess it is not.


If you read the documentation you would see that this is not a "bug" as you put it. Hibernate does have an Assigned generator class.

http://www.hibernate.org/hib_docs/api/n ... igned.html


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 25, 2004 3:25 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
gpani wrote:
If you read the documentation you would see that this is not a "bug" as you put it. Hibernate does have an Assigned generator class.


No need to get snippy. I read the docs, and this is not the problem I have. What I was hoping was that when I create a new instance of an object, and set my own primary key (user-assigned), that when I call saveOrUpdate, that it would do a select query in the DB, see if that primary key exists, if so, update it, if not, save it. Maybe there are other problems with this approach, but it seems useful. Obviously I can easily work around this so its no big deal. Thanks! Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 10:21 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
mchyzer wrote:
No need to get snippy.


I am sorry you feel that way. I wasn't being snippy. Just pointing out that some things that may appear to be "bugs" aren't necessarily that. Just a matter of interpretation I guess. Be glad Gavin didn't catch you calling things bugs without adequate research. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 3:47 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
If you're using <version> you can get your assigned objects to work with saveOrUpdate(). It works, I'm using it.

Read the 4th paragraph (starts with "You may instead...")
http://www.hibernate.org/hib_docs/refer ... ompositeid

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 5:26 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
gus wrote:
If you're using <version> you can get your assigned objects to work with saveOrUpdate().


Thats a good idea. Also, I now understand why this is not a bug or even desired functionality, since you would not want to store a transient object if you arent sure it hasnt been updated in the DB in the meantime. So the timestamp version (and unsaved-value) is a good approach... Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 6:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can also use select-before-update=true


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 6:10 pm 
Newbie

Joined: Wed Aug 04, 2004 5:48 pm
Posts: 2
michael wrote:
You can also use select-before-update=true


Can you please give me a descriptive answer?

I have my composite-id tag as below:

<composite-id name="refSourceDataCompKey" class="com.xyz.ReferenceSourceDataCompositeKey" >
<key-property name="sourceSystemId" column="source_system_id_c"/>
<key-property name="recordKeyId" column="record_key_id_c"/>
<key-property name="sourceSystemName" column="source_system_name_c"/>
</composite-id>


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