-->
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.  [ 3 posts ] 
Author Message
 Post subject: org.hibernate.NonUniqueObjectException
PostPosted: Wed May 20, 2009 8:03 pm 
Beginner
Beginner

Joined: Tue Feb 03, 2009 4:33 pm
Posts: 21
Okay, having conquered the first newbie hurdle of LazyInitializationException, now I am faced with the second-most-common issue: NonUniqueObjectException in a cascade saveOrUpdate.

I have a Submission table, that contains many child Product records, and each Product contains many ProductFiles.

In my app, I load a Submission from the database using Hibernate, then populate it's children and grandchildren with Products and ProductFiles created at run-time, then attempt to saveOrUpdate the Submission. All my hbm.xml mappings have default-cascade="save-update", so Hibernate attempts to insert the new Products and ProductFiles (or at least it should).

At this point, I receive a "NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.testProject.entities.ProductFile#0]".

I should note that the ProductFile entity's identifier is an Integer value, with a default (unsaved) value of zero. So here, it looks as though Hibernate is seeing a bunch of ProductFiles with the same ID (zero, since they are all unsaved), and is freaking out.

So how can I tell the Hibernate Engine that these objects are unsaved, and therefore not "identical"?


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Wed May 20, 2009 10:59 pm 
Beginner
Beginner

Joined: Tue Feb 03, 2009 4:33 pm
Posts: 21
A little digging has uncovered that my hbm.xml files, which were generated by the mysterious, undocumented voodoo of the ANT hbm2hbmxml task, contain a <generator class="assigned"> for every class. I'll change this to "native", and see if that makes a difference...


[drum roll...]

Hmm. Nope. Now I'm getting "org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()".

So how can I tell Hibernate that these IDs will be generated by the Database engine (in this case a MySQL auto-incremented field)??

UPDATE: Okay, got it! I'll use <generator class="identity"/>


Top
 Profile  
 
 Post subject: Re: org.hibernate.NonUniqueObjectException
PostPosted: Thu May 21, 2009 7:51 am 
Beginner
Beginner

Joined: Tue Feb 03, 2009 4:33 pm
Posts: 21
So I got all that working now, but I'm still getting a NonUniqueObjectException for something else, now.

Each Product has a child ProductType. So for each ew Product I create (and I'm creating several at once), I get an existing product type from the database, and associate it with the new product (Product.setProductType(Session.get(ProductType.class,3))). I'm making a separate call to Session.get() here, which may be loading multiple ProductType objects with the same ID(?)

The associated ProductType hasn't changed, so it's not dirty, so when I call session.save(Product), Hibernate shouldn't try to update the "child" productType, correct? And yet I'm getting "NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.testproject.entities.ProductType#3]"

What am I doing wrong here? Why does Hibernate think the ProductType instance is dirty?


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