hi,
i'm trying to do a full CRUD using a collection of detached objects.
the ids of the items are user defined (not generated).
the collection contains entries that already exist in the database and entries that don't exist in the database.
existing entries should be updated, new entries should be inserted.
any entries that exist in the database but not in the collection should be deleted.
so far i have been trying to delete all entries from the database first, then perform a saveOrUpdate on each item in the collection:
Code:
for (Object entity : session.createCriteria(type).list())
session.delete(entity);
for (Object entity : collection)
session.saveOrUpdate(entity);
however this results in the error "a different object with the same identifier value was already associated with the session".
i assumed this was because the delete loop was loading an object into the session, and i was then attempting to save a different object with the same id later, so i changed the code so that i evicted each delete entity:
Code:
for (Object entity : session.createCriteria(type).list())
{
session.delete(entity);
session.evict(entity);
}
for (Object entity : collection)
session.saveOrUpdate(entity);
and now i'm getting "org.hibernate.AssertionFailure: possible nonthreadsafe access to session"
what is the least exhaustive method of performing this kind of operation.
note: i need this to happen within one transaction in-case anything doesn't work i don't want the data load to have any effect.
thanks, paul.