Hi Dan, Thanks for your time most appreciated.
1. Option 1 as you may agree, not always is a good thing to do so I would drop that. 2. Option 2 - I have tried this in the following manner. As part of the look up for the Subjects via the DAO, I iterate through the list of Person.Phones collection and assign them into a Collection<Phones> and set it into a value Object with has a List. This is because i cannot use the Set in the PageableListView. In doing so, I have forced the entities in the collection/proxy to be intialised isn't it? Looks like even with this it beats me. 3. Option 3 - I have to read up more on how I can use this code/or something similar, we use Spring for DI. Further, each time I want to view a Person detail, I do a second look up when the user clicks from a list of Persons. I send issue a lookup into the DAO to get the Person's details afresh(the exact same method I used to list all Subjects in the first place), so this again would have refreshed the Phones collection on the Person in context. I will try to track it down I guess it has to do with session anyway. I also use the CPM to hold the Model for the whole page. Not a LDM. Thanks again for the time Cheers Niv On Thu, Dec 2, 2010 at 1:47 PM, Dan Retzlaff <[email protected]> wrote: > Hi Nivedan, > > Even though the subsequent requests have a Session open, the entities with > the uninitialized collections don't know about it. I'm sure if you track it > down, you can explain the "intermittent" behavior by prior access to the > collection when the original session is open. > > I'd say you can either (1) configure Hibernate to load the collections to > load unlazily, (2) manually access the collections to force them to > initialize in the specific cases you're encountering LIEs, or (3) employ > some kind of AOP hack to reinject the new session right before the > collection is accessed. They're all kind of ugly, and I've never heard of > anyone else doing the last, but it's been working well for my team. > > For your reference, here is the AspectJ aspect I wrote. (We use Guice for > dependency injection.) > > /** > * Reattaches entities whose lazy collections are about to be initialized > * <p> > * Can we keep track of all lazy relationships that get initialized, and > * uninitialize them at the end of the request? This would prevent > referenced > * entities from being serialized and replicated (unless separate > references > * were created to them). > * > * @author dan > */ > @Aspect > public class ReattachAspect { > private static final Logger LOG = Logger.getLogger(ReattachAspect.class); > > private Provider<Session> sessionProvider; > > @Before("call(public final void > org.hibernate.proxy.AbstractLazyInitializer.initialize()) && > target(initializer)") > public void reattachLazyInitializer(LazyInitializer initializer) { > if (initializer.getSession() == null && sessionProvider != null) { > if (LOG.isDebugEnabled()) { > LOG.debug("reattaching session to lazy initializer for " + > initializer.getEntityName()); > } > Session session = sessionProvider.get(); > initializer.setSession((SessionImplementor) session); > } > } > > @Before("call(private void > org.hibernate.collection.AbstractPersistentCollection" > + ".throwLazyInitializationExceptionIfNotConnected()) && > target(collection)") > public void reattachPersistentCollection(PersistentCollection collection) { > SessionImplementor session = ((AbstractPersistentCollection) > collection).getSession(); > if ((session == null || !session.isOpen()) && sessionProvider != null) { > if (LOG.isDebugEnabled()) { > LOG.debug("reattaching session to collection"); > } > > session = (SessionImplementor) sessionProvider.get(); > CollectionPersister persister = > session.getFactory().getCollectionPersister(collection.getRole()); > > collection.setCurrentSession(session); > session.getPersistenceContext().addInitializedDetachedCollection(persister, > collection); > } > } > > @Inject > public void setSessionProvider(Provider<Session> sessionProvider) { > this.sessionProvider = sessionProvider; > } > } > > > On Wed, Dec 1, 2010 at 9:23 PM, Nivedan Nadaraj <[email protected] > >wrote: > > > Hi All > > > > I am guessing this is more of a Hibernate thing/issue but if some one has > > encountered this and has a explanation that I can probably use from the > > Wicket front would be great. > > > > https://forum.hibernate.org/viewtopic.php?f=1&t=1008473 > > > > > > I have a LazyIntializationException when i page through some items. I use > > the PageableListView, the List item(s) are entities that are retrieved > via > > an association Person.phones which is a Set type. > > The funny thing is, the LIException is intermittent. I am also using > > OpenSessionInViewFilter. Any thoughts? > > > > By the way the this is the load() implemenation, I have set the Model > > Object's phoneList with a list of values fetched via the Service->DAO. I > > have used this with other entities without association and it works but > I > > guess is a different scenario(not associations) > > > > Model = new LoadableDetachableModel<Object>() { > > @Override > > protected Object load() { > > return containerForm.getModelObject().getPhoneList(); > > } > > }; > > } > > > > If someone has any thoughts would appreiciate hearing from you. > > > > > > Cheers > > >
