简体   繁体   中英

Lazy loading working in non-transactional class/method in Hibernate

I am working in Spring-Hibernate application. Flow is as usual: Controller --> Service --> DAO .

I have annotated Service layer class with @Transactional , thereby marking every method transactional in that class.In service class, I made a DAO call to get some domain object and then converting it into DTO/VO object which will be passed to controller.For converting domain object to DTO , I have written another custom static class(class having only static methods) like ObjectMapper which will do this conversion.

Now, domain object has some child object( One to Many ) which is lazily loaded. So, when in ObjectMapper , i access that child getter method, an extra database call is issued, which is working fine. What I don't understand is that since ObjectMapper is not transactional , I was expecting some exception to be thrown like Session is closed while making database call to fetch child object from database.I am using getCurrentSession of Session Factory in DAO .

Can someone please explain me this behavior?

我想您要么从事务性Service方法调用ObjectMapper(否则就应该调用),否则,可能启用了“ hibernate.enable_lazy_load_no_trans”,从而使休眠会话保持打开状态

只要在DAO的事务方法中进行了对转换您的对象的静态类/方法的调用,该会话仍处于打开状态,并将用于数据库调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM