java - Why does EntityManager.merge() prevent LazyInitializationException while EntityManager.find() don't? -


given following situation in web application:

// entitymanager em, 1 per request spring's openentitymanagerinviewfilter // parent oldparent, previous request (and therefore persistence context) parent parent = em.find(parent.class, oldparent.getid()); list<child> children = parent.getchildren(); // mapped collection lazyloading (child child : children) {      ... 

the call of list iterator causes lazyinitializationexception. confusing, because fetching of list of children occurs in same persistence context (or wrong?). but, using merge(), works. if 2 request sharing 1 persistence context.

parent parent = em.merge(oldparent); list<child> children = parent.getchildren(); (child child : children) {      ... // no exception!! 

what error in reasoning?

addition
i've proven error not caused parent.getid(). part of stacktrace:

at org.hibernate.collection.persistentlist.iterator(persistentlist.java:138) 

that means iterator causes problem. , it's getting more weird - i've checked in first case (with find()), select statement issued hibernate retrieve new object database, not cache of persistence context.

addition2
here's bit more stacktrace:

org.hibernate.lazyinitializationexception: failed lazily initialize collection of role: edeka.sw.phb.model.chapter.subchapters, no session or session closed     @ org.hibernate.collection.abstractpersistentcollection.throwlazyinitializationexception(abstractpersistentcollection.java:380)     @ org.hibernate.collection.abstractpersistentcollection.throwlazyinitializationexceptionifnotconnected(abstractpersistentcollection.java:372)     @ org.hibernate.collection.abstractpersistentcollection.initialize(abstractpersistentcollection.java:365)     @ org.hibernate.collection.abstractpersistentcollection.read(abstractpersistentcollection.java:108)     @ org.hibernate.collection.persistentlist.iterator(persistentlist.java:138)     @ java.util.collections$unmodifiablecollection$1.<init>(collections.java:1022)     @ java.util.collections$unmodifiablecollection.iterator(collections.java:1021)     //... followed line of foreach. 

i think problem not in iterating part because iterate new loaded parent. problem has .getid() seems not loaded merge works because dont call .getid()


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -