0

I have this situation (details at the bottom):

  • X is a hibernate entity
  • X.y is a lazy-loaded reference to Y, another hibernate entity
  • Y.z is a lazy-loaded, cached collection of hibernate entities Z

In a post-transaction Spring bean, within open-session-in-view, I'm accessing X.y.z and get "org.hibernate.HibernateException: Unable to resolve owner of loading collection [...] for second level caching". Debugging into Hibernate 4.3.8.Final I find that X.y is lazily loaded using a temporary session and is therefore not available in the "original" StatefulPersistenceContext, where CollectionLoadContext is looking for it.

So am I doing something that's just not done? Do I need to explicitly trigger this lazy-loading beforehand or remove the caching?

public class Teachable {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_org_element_sch", nullable = false)
    protected ElementSch element;
}

public class ElementSch {
    @OneToMany(targetEntity = ElementConnectionSch.class)
    @JoinColumn(name = "fk_org_element_parent")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Collection<ElementConnectionSch> childConnections;
}

public class ElementConnectionSch {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="fk_org_element_parent")
    private ElementSch parent;
}

After the transacation has been committed, but still within open-session-in-view, I'm calling Teachable.getElement().getChildConnections() and getting the HibernateException. From debugging I can see ElementSch and ElementConnectionSch being lazily loaded in separate sessions, i.e. main session has loaded Teachable and two separate temporary sessions are being used for ElementSch and ElementConnectionSch.

Mirvnillith
  • 405
  • 5
  • 18

1 Answers1

0

This has been identified as an issue on our part due to Hibernate version upgrade:

We use Spring WebFlow and put Hibernate entities into flow scopes. In order for this to work properly across multiple calls within the flow we inject re-attaching wrappers to entities and collections so that they pick up the session of the ongoing call instead of using the one they were created in, now closed.

With the version upgrade came changes to how this injection was performed and, although working, it was applied before the original mappings were loaded, i.e. we looped over empty iterators and injected nothing.

Mirvnillith
  • 405
  • 5
  • 18