I am trying to test a entity value in Junit, the entity has many-to-many relation with lazy fetch type.
When I try to expand this in Junit I am getting the following error. Please hightlight if my understanding is wrong, all I need is to have the session opened in Junit and load this object.
SEVERE: failed to lazily initialize a collection of role: com.example.stock.model.Stock.locations, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.stock.model.Stock.locations, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)
at com.example.common.StockManytoManyTest.testGetStock(StockManytoManyTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
Annotate your integration test class or method with
@Transactional. See also: 10.3.5.4 Transaction management.