I am using JPA(with Hibernate 4.3.3 as persistence provider ) along Spring (3.2.2) , all my fields are loading fine but when I am trying to access my Collection it\'s throwing t
Like @Zeus said, this has been answered many, MANY, times before. You are having this problem in your test class because your transaction begins and ends at your service call:
Category parentCategory=serviceCategory.findCategoryById(2l);
Recall from Hibernate documentation that lazy loading only works within a Hibernate Session (in this case, the hibernate session begins and ends with your service call). You can't reconnect to the hibernate session (simply) to initialize the collection.
I'm not exactly sure what you mean when you want to solve it "in spring." Since this isn't a Spring issue. Essentially the two ways to resolve this are to load the collection within the hibernate session you load the parent in or execute a separate fetch outside of the original hibernate session.
The problem is that when this method call returns:
Category parentCategory=serviceCategory.findCategoryById(2l);
Here you are no longer in a @Transactional
context. this means that the session linked to parentCategory got closed. now when you try to access a collection linked to a closed session, the No Session
error occurs.
One thing to notice is that the main method runs outside any spring bean and has no notion of persistence context.
The solution is to call the parentCategory.getAllParentCategoryrefs()
from a transactional context, which can never be the main method of your application.
Then reattach the parentCategory to the new persistence context, and then call the getter.
Try for example to pass the parentCategory back to a transactional method of the same service:
serviceCategory.nowItWorks(parentCategory);
where the method on the service is transactional:
@Transactional(readOnly=true)
public void nowItWorks(Category category) {
dao.nowItWorks(category);
}
And in the DAO:
public void nowItWorks(Category category) {
Category reattached = em.merge(category);
System.out.println("It works: " + reattached.getAllParentCategoryrefs());
}
Try using fetch=FetchType.EAGER
, it will work
use @Fetch(FetchMode.SELECT)
and @LazyCollection(LazyCollectionOption.FALSE)
on your domain set collection , it will work