java - How to make Hibernate 4 stop throwing "Deleting detached instance"? -
i have use hibernate 4.1.7 in project. unfortunately, it's not me move newer version.
in context, have master-detail situation. so, load master object , show on web page, details. can change both master , details way like, including new details. saving code follows:
@override @transactional public void save(entity entity) { session session = entitymanager.unwrap(session.class); session.saveorupdate(entity); session.flush(); }
problem comes when try delete detail. code below shows how done:
mymaster.getdetails().remove(mydetail);
i expect hibernate track changes on master object (it lost detail instance list member), when call save()
method throws java.lang.illegalargumentexception: removing detached instance xxxx#yyyy
.
i understand concept if trying remove detached instance, don't understand why removed instance being considered detached hibernate.
any ideas?
tia!
all need make work way expect (to let hibernate check entire object graph) this:
@override @transactional public entity save(entity entity) { return entitymanager.merge(entity); }
of course, make sure merge
operation cascaded master details (cascadetype.merge
or cascadetype.all
) , orphanremoval
set details collection. if orphanremoval
not suitable, have remove details explicitly (otherwise hibernate delete associated children when stopped being associated parents, not desirable).
also, make sure use result of merge
operation afterwards returns copy of passed-in detached instance (passed-in instance not modified). important when persisting new instances because ids set in copy.
however, pay penalty of reloading object graph db, but, if want done automatically, hibernate has no other way check has changed, other comparing current state in db.
now explanations of observed in attempts:
the master instance save detached. load in 1 transaction (persistence context/session), , save in another.
it works fine
saveorupdate
when update it:either save(object) or update(object) given instance, depending upon resolution of unsaved-value checks (see manual discussion of unsaved-value checking).
parameters:
object - transient or detached instance containing new or updated state
as stated in doc, intended work detached , transient instances.
you tried merging, got exception telling there object same id. explanation tried this:
@override @transactional public void save(entity entity) { entity = entitymanager.merge(entity); session session = entitymanager.unwrap(session.class); session.saveorupdate(entity); session.flush(); }
then exception thrown described in
update
javadoc:update persistent instance identifier of given detached instance. if there persistent instance same identifier, exception thrown.
so, merged instance current persistence context, , tried
saveorupdate
(which delegatedupdate
) , resulted in exception, must not update detached instance while there persistent 1 in current persistence context (otherwise persistent 1 become stale).you don't have this, merge, , @ end of transaction hibernate dirty-check objects , flush changes db automatically.
Comments
Post a Comment