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.