EntityOne has a reference to EntityTwo, and EntityTwo has a reference to EntityThree. All three relationships are defined as follows:
Code:
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "XX", unique = false, nullable = false, insertable = true, updatable = true)
In my code, I am trying to get to EntityThree this way:
Code:
entityOne.getEntityTwo().getEntityThree()
If I use FetchType.EAGER, this works perfectly. If I use LAZY, entityOne.getEntityTwo() returns a proxy, and getEntityThree() then fails:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at com.ibi.entity.price.OperatingSystem$$EnhancerByCGLIB$$cee9d0f8.toString(<generated>)
Originally I had FetchType.EAGER, but I thought I'd switch to LAZY to improve performance (as EntityOne has a number of @ManyToOne relationships, and I usually don't need them all at any one time.) Clearly I am missing something elemental in how LAZY should work, and/or my coding technique is incorrect. Can someone point me in the right direction?