I have a class of User that has a list (collection) of Projects:
@OneToMany(mappedBy = "user_owner_id")
private Collection<Project> project = new ArrayList<>();
And at some point, I want to see the list of projects and I fetch them:
Session sessionF = sessionFactory.openSession();
sessionF.beginTransaction();
user = sessionF.get(User.class, user.getId());
sessionF.getTransaction().commit();
List<Project> projects = (List<Project>) user.getProject();
sessionF.close();
If I don't do something with projects it throws the error: org.hibernate.LazyInitializationException: could not initialize proxy – no Session
But if I am adding a int projectCount = projects.size(); it works just fine. Why does that happen and how can I resolve it without playing with projects here?
P.S. After I am closing the session I am just passing it through the HttpServletRequest and then going with a for loop over it in a jsp file.
user.getProject()load it to theList<Project> projects? This is the part where I am confused. Why do theprojects.size()load it then?