Using the lazy loading of Hibernate?

I have a domain object that has an attribute, which is a collection containing another domain object. This is done using sleep mode matching (which ultimately performs the join in another table). Hibernate, by default, seems to lazily instantiate this collection. This turns out to be wonderful, because, depending on what I need to display, I do not always need the collection that needs to be downloaded.

My problem is this: when writing my sleep requests (in my DAOs), I use the following to open / close a session:

Session session = getSessionFactory().openSession();
//query goes here using the session var
session.close();

The problem is that when Hibernate finally arrives at the lazy loading of my collection, it Sessionhas long been closed! How can I get around this? I guess I need to close the session as I do ...

This is the error I get:

SEVERE: failed to lazily initialize a collection of ...
0
source share
2 answers

If this happens inside the webapp, it’s easy to set OpenSessionInViewInterceptor or OpenSessionInViewFilter for this . They delay session closure until the entire request is completed, which allows you to navigate through lazy associations when rendering the view.

, , , . , , , . . "fetch joins" Hibernate.

"" , . .

+5

, DAO. DAO, - . , Hibernate Reference " " :

" : ".

, . (, ) ( () ). , (= ) , .

, . , , , .

+2

Source: https://habr.com/ru/post/1541560/


All Articles