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.