How does spring allow lazy loading?

If you have a call in the Dao method, for example (pseudo-code):

return..getHibernateTemplate (get by id);

Now say that the object has a lazy collection. Once you return from your Dao using the hibernateTemplate helper method, why does the session stay in scope and allow you to lazily load the collection?

Is the session initialized and committed globally for each request?

Update

Please explain exactly where the getcurrentsession call is made, and when is it actually closed / fixed?

As far as I understand, the spring structure should handle the session life cycle, where does it do it? at what point in the query life cycle?

It also handles the Unit of work, where / how?

+3
source share
4 answers

Once you return from your Dao using the hibernateTemplate helper method, how does the session stay in scope and allow you to lazily load the collection?

Because it Sessionis not yet closed, and your essence, thus, remains constant (in contrast to the state of the object A separate object). Until your face is removed from you, you can use lazy assemblies and proxies. See Chapter 10.1. The Hibernate object indicates for more detailed information about these states (it is very important to understand them and the terminology used).

?

-, , . javadoc HibernateTemplate:

Lazy loading Hibernate, , OpenSessionInViewFilter/Interceptor.

javadoc OpenSessionInViewFilter OpenSessionInViewInterceptor, , , Hibernate "Open Session in View" ".

, , getcurrentsession, /?

, . HibernateTemplate, doExecute(), . / OpenSessionInViewFilter/Interceptor, .

, spring , ? ?

, : .

, /?

, . Hibernate - . , , .

PS: , , . spring Hibernate javadoc. , , , , .

+6

Hibernate , , . .

sysout getSomeCollection().getClass(), , Hibernate , javadocs/source, , .

- // , . , ?;)

+1

?

, ... ( )

, , getcurrentsession, /?

: org.springframework.orm.hibernate3.HibernateTemplate

... Spring Hibernate. HibernateTemplate ( ThreadLocal) , HibernateTemplate # execute *.

As far as I understand, the Spring framework should handle the session life cycle, where does it do it? at what point in the query life cycle?

Also performed using HibernateTemplate and ...

It also handles the Unit of work, where / how?

Executed using the HibernateTransactionManager, if you have a Context configured in your application.

+1
source

If it was a web application, I would use OpenSessionInViewFilter

0
source

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


All Articles