-->
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.  [ 6 posts ] 
Author Message
 Post subject: Why this NonUniqueObjectException?
PostPosted: Fri May 20, 2005 7:51 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I've run into the exception below and I can't work out why I get it in one situation but not another:

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 66, of class: testing.om.Product

This is in the context of a Struts/Spring/Hibernate web application. I store a Product record in the HttpSession at the point when the user moves to a page to add or edit a StockItem (the Product contains a collection of StockItems). When they go to save the StockItem, I retrieve the Product from the HttpSession, delete the old associated StockItem from product.stockItems collection (if it's not new), add the new one and store the product. Editing an existing stock item this works just fine, no problem. When I add a new stock item, though, I get the exception about the product already existing. Why the difference? I use the same method for processing and storing the stock item record in each case. And why does Hibernate think I'm trying to create a new Product with the same identifier (in this case) rather than just updating an existing one? (I am using saveOrUpdate).

Hope someone can shed some light on this.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 3:16 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Hibernate does not think that you are creating a new Product with the same identifier. It is telling you have associated two instances of the same persistent object in the scope of the session.

Maybe you are rassociating objects using session.lock() as you go through you page flow and calling the backend. If this is the case, make sure that you are not always reusing the same session.

Good luck,
Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 3:22 pm 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
vgiguere wrote:
Hibernate does not think that you are creating a new Product with the same identifier. It is telling you have associated two instances of the same persistent object in the scope of the session.

Maybe you are rassociating objects using session.lock() as you go through you page flow and calling the backend. If this is the case, make sure that you are not always reusing the same session.

Good luck,
Vincent.


Thanks. What puzzles me is that the way the object is stored is identical for edits (which work) and adds (which don't). Could it be something to do with Spring's OpenSessionInViewFilter?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 22, 2005 10:57 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
To answer your question, I am not familiar with Spring yet.

Maybe you could post the code where you retrieve the Product and reassociate it to the session prior to saving it

Are you in a multi-layer environment? Do you reassociate objects to the session when you cross your layers, or do you have the session available on your front end?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 9:47 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
vgiguere wrote:
To answer your question, I am not familiar with Spring yet.

Maybe you could post the code where you retrieve the Product and reassociate it to the session prior to saving it

Are you in a multi-layer environment? Do you reassociate objects to the session when you cross your layers, or do you have the session available on your front end?


Sorry, I was away for a while, just got back.

I discovered the source of the problem. It was that the StockItem's association to Product had "cascade=save-update" instead of 'cascade=none' (i.e., the many-to-one definition in the StockItem mapping). As soon as this was changed to 'none', the problem went away. Still not sure exactly why this should be so, though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 9:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You guys are living dangerous. The exception has a very clear and important meaning: You are doing something wrong in your transaction processing and/or in the way you handle object identity and object instances. You have to understand what merge() (or in Hibernate2 saveOrUpdateCopy) is good for. Then read everything you can find again about the Session's scope and identity guarantees. Then understand Detached Objects. Then fix your problem right. No, Spring won't help, just complicate the situation.


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