-->
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: detached collection update problems
PostPosted: Wed Jan 19, 2011 10:34 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
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.


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.