Dan, Thanks mate. I am yet to digest all that. I am going to get back to you and see if it all helped.
Thank you for the time Cheers niv On Fri, Dec 3, 2010 at 1:06 AM, Dan Retzlaff <dretzl...@gmail.com> wrote: > Yes, copying entities from the entity's association collection into another > collection will initialize the collection. If you're still getting an LIE, > there may be another association at play (a child of Phone?). > > Note that I don't fully endorse the session reattachment aspect I posted. > Not only is weaving those Hibernate classes a little tricky and a lot of > hacky, it can cause undesirable amounts of entities to be added into the > session. James' suggestion of putting the collection behind a Wicket model > is more elegant. To this end, you might develop a utility which, given a > list of Persons, returns an IModel<List<Person>> while storing only their > IDs in the session. In the following code, BasicDao.getIdentifier() and > BasicDao.get() simply map to Hibernate Session methods of the same name for > entity class T. > > public static <T> IModel<List<T>> createListModelFromObjects(final > BasicDao<T> dao, List<T> objectList) { > final List<Serializable> idList = new > ArrayList<Serializable>(objectList.size()); > for (T object : objectList) { > idList.add(dao.getIdentifier(object)); > } > return new LoadableDetachableModel<List<T>>(objectList) { > @Override > protected List<T> load() { > return loadList(dao, idList); > } > }; > } > > private static <T> List<T> loadList(BasicDao<T> dao, List<? extends > Serializable> idList) { > List<T> loadList = new ArrayList<T>(idList.size()); > for (Serializable id : idList) { > loadList.add(dao.get(id)); > } > return loadList; > } > > > On Wed, Dec 1, 2010 at 10:26 PM, Nivedan Nadaraj <shravann...@gmail.com > >wrote: > > > 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 <dretzl...@gmail.com> > 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 <shravann...@gmail.com > > > >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 > > > > > > > > > >