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
>

Reply via email to