简体   繁体   English

延迟加载在Hibernate的非事务类/方法中工作

[英]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.

相关问题 是否可以将整个@Transactional类的一个方法标记为非事务性的 - Is it possible to mark one method of an entire @Transactional class as non-transactional 如何从事务方法调用非事务方法 - How to call non-transactional methods from a Transactional method 例外-非交易 - Exception - non-transactional 当来自另一个事务的延迟加载的 Hibernate 对象被传递给方法时,Spring @Transactional 不起作用 - Spring @Transactional not working when Hibernate object with lazy loading coming from another transaction is passed down to method Spring 3 MVC Hibernate 3.5.4 hibernateTemplate没有关闭连接(非事务性) - Spring 3 MVC Hibernate 3.5.4 hibernateTemplate not closing connections (non-transactional) Spring,@ Transactal和Hibernate Lazy Loading - Spring, @Transactional and Hibernate Lazy Loading 事务回滚后调用非事务方法 - Calling non-transactional method after transaction is rolled back 在@Transactional方法中加载惰性集合 - Loading lazy collection in @Transactional method 在另一个实例中从事务方法调用 spring 非事务方法时,事务是否得到传播? - When calling a spring Non-Transactional method from a Transactional Method in another instance, does the transaction get propagated? Spring MVC + Hibernate:没有Hibernate Session绑定到线程,配置不允许在这里创建非事务性的 - Spring MVC + Hibernate: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM