[英]Lazy loading working in non-transactional class/method in Hibernate
I am working in Spring-Hibernate
application. 我正在使用
Spring-Hibernate
应用程序。 Flow is as usual: Controller --> Service --> DAO
. 流程照常:
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. 我用
@Transactional
注释了Service层类,从而标记了该类中的每个事务方法。在service类中,我进行了DAO
调用以获取某些域对象,然后将其转换为DTO/VO
对象,该对象将传递给controller。将域对象转换为DTO
,我编写了另一个自定义静态类(仅具有静态方法的类),例如ObjectMapper
,它将进行此转换。
Now, domain object has some child object( One to Many
) which is lazily loaded. 现在,域对象有一些子对象(
One to Many
)被延迟加载。 So, when in ObjectMapper
, i access that child getter method, an extra database call is issued, which is working fine. 因此,在
ObjectMapper
,当我访问该子getter方法时,将发出一个额外的数据库调用,该调用工作正常。 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
. 我不明白的是,由于
ObjectMapper
不是transactional
,所以我期望在抛出数据库调用以从数据库中获取子对象时Session is closed
,从而引发一些异常。我正在DAO
使用Session Factory
getCurrentSession
。
Can someone please explain me this behavior? 有人可以向我解释这种行为吗?
我想您要么从事务性Service方法调用ObjectMapper(否则就应该调用),否则,可能启用了“ hibernate.enable_lazy_load_no_trans”,从而使休眠会话保持打开状态
只要在DAO的事务方法中进行了对转换您的对象的静态类/方法的调用,该会话仍处于打开状态,并将用于数据库调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.