-->
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.  [ 1 post ] 
Author Message
 Post subject: Delete and Insert - NonUniqueObjectException
PostPosted: Tue May 17, 2011 10:55 pm 
Newbie

Joined: Tue Apr 12, 2011 7:33 pm
Posts: 4
Hi guys,

We're trying to do something pretty simple which is essentially DELETE all entries given an ID. And then Inserting new values with the same ID.

However when we are doing this we get the following exception:
javax.persistence.PersistenceException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:


How we are doing it is as follows:
Code:
List<TableValue> values =            em.createQuery("select c from table c where c.etce = :etc...)
            .setParameter(...)
            .setParameter(...)
            .getResultList();

em.remove(values);
//At this point we check the database and indeed the values are deleted from the DB.

//Now we attempt to re-insert the values

TableValue new = new TableValue();
            new.setId(..something that was previously used);
            new.setCommentValue("blah");
            em.persist(new);

em.commit();


We have tried preforming commits between the two processes but it still gives the same problem. We have also tried detaching after the remove but it again has the same exception.

The only way in which we have able to get around this issue is by implementing it in the following way:

Get the list of existing rows
Create a list of the newly inserted values
Any matching ID's we run a em.merge()
Any nonmatching ID's in the existing rows will get deleted.

This is alot of manual processing where essentially all we want to do is delete all and then re-insert the new values.

Any help would be greatly appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.