Consider the data model from "Java Persistence with Hibernate" where a Bid
has a lazy association to Item
:
@ManyToOne(optional = false, fetch = FetchType.LAZY) // NOT NULL
@JoinColumn(name = "ITEM_ID") // Actually the default name
protected Item item;
and then the following snippet that tries to load a Bid
through a StatelessSession
and then access the associated Item
:
Bid bid = (Bid) statelessSession.get(Bid.class, bidId);
assertNotNull(bid.getItem());
assertEquals(bid.getItem().getName(), "Bike");
This throws a LazyInitializationException
, even though the session is still active. Can we infer from here that lazy loading is not supported in conjunction with StatelessSession
?
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session is disconnected
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:154)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:266)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:68)
at org.jpwh.model.simple.Item_$$_jvst6d3_0.getName(Item_$$_jvst6d3_0.java)
at org.jpwh.test.stateless.CrudWithAssociations.lambda$fetchLazyAssociationForStackOverflow$6(CrudWithAssociations.java:94)
at org.jpwh.test.stateless.CrudWithAssociations$$Lambda$2/310350177.call(Unknown Source)
at org.jpwh.env.StatelessSessionTest.transaction(StatelessSessionTest.java:21)
at org.jpwh.test.stateless.CrudWithAssociations.fetchLazyAssociationForStackOverflow(CrudWithAssociations.java:90)
NOTES:
- The code works fine if I change the association to
FetchType.EAGER
- I've tested this on Hibernate 5.0.6.