简体   繁体   English

Spring + Hibernate:无法初始化没有会话的代理-DAO上发生异常时该怎么办?

[英]Spring+Hibernate: could not initialize proxy no Session - What to do when an exception occured on a DAO?

I got could not initialize proxy no Session exception on my JSP pages. could not initialize proxy no Session在JSP页面上could not initialize proxy no Session异常。

Here is what I do; 这是我的工作;

There are DAOs for Hibernate. 有用于休眠的DAO。 These are not special classes. 这些不是特殊的类。 They just have autowired instances of Hibernate SessionFactory. 他们只是具有自动连接的Hibernate SessionFactory实例。 Yes I use pure Hibernate to do my DB access. 是的,我使用纯Hibernate进行数据库访问。

The functions on DAO classes are annotated with @Transactional(rollBackFor=Exception.class) and My spring configurations prepared accordingly. DAO类上的函数用@Transactional(rollBackFor=Exception.class)注释,并相应地准备了我的spring配置。 Also my entities/controllers are also configured with Annotations etc. 此外,我的实体/控制器也配置有注释等。

I dont have a seperate Service classes layer. 我没有单独的服务类层。 My Services and DAOs are overlapped. 我的服务和DAO重叠。 they are same. 他们是一样的。 If a Service class need to access DB they are just annotated with @Repository rather than @Service 如果Service类需要访问数据库,则仅使用@Repository而不是@Service注释。

Until this error, everything was working perfectly. 直到出现此错误,一切都运转良好。 (I have hundreds of classes/DAOs/entities etc) (我有数百个类/ DAO /实体等)

Now, I need to call two DAO/Service functions at one of my controllers. 现在,我需要在我的一个控制器上调用两个DAO / Service函数。 I only call second DAO function when the first one throws an exception. 当第一个DAO函数抛出异常时,我才调用它。

I need to throw exceptions based on some conditions to rollback the changes made with first DAO call. 我需要根据某些条件抛出异常,以回滚第一次DAO调用所做的更改。 The second call just to show some information from DB to the user who entered wrong input. 第二个调用只是为了向输入错误输入的用户显示DB的一些信息。

I dont know why but the second DAO call is working, its gets classes from DB. 我不知道为什么,但是第二个DAO调用正在工作,它从DB获取类。 then I forward this classes on to my Model attribute on my controllers so my JSPs can displays them. 然后将这些类转发到控制器上的Model属性,以便JSP可以显示它们。

But when my JSP pages tries to access attributes of these classes I got the could not initialize proxy no Session exception. 但是,当我的JSP页面尝试访问这些类的属性时,我得到了could not initialize proxy no Session异常的信息。

So here is my question, what should I do to avoid this errors. 所以这是我的问题,我应该怎么做才能避免这种错误。

Should I do something with hibernate transaction/session on the first DAO call? 我应该在第一个DAO调用中对休眠事务/会话执行某些操作吗? Because I do nothing. 因为我什么都不做。 I just get a session and do get/save/update etc on it. 我只是得到一个会话,并获取/保存/更新等。 I think @Transactional attribute will handle the transaction issues and @Repository attribute handles the session issues. 我认为@Transactional属性将处理事务问题,而@Repository属性将处理会话问题。


I have already tried lazy/eager loading issue. 我已经尝试过延迟/渴望加载问题。 I have already put OpenSessionInViewFilter filter on my configurations. 我已经将OpenSessionInViewFilter过滤器放在我的配置中。

But none of them solved my problem. 但是他们都没有解决我的问题。

Any advises/thoughts? 有什么建议/想法吗?

I solved the problem now :) 我现在解决了问题:)

The problem was my misunderstanding of how Hibernate with Spring Framework works unfortunately :) 问题是我对Hibernate with Spring Framework的工作方式感到遗憾:)

The exception occurs is in the JSP page when I was comparing the values of two different Hibernate proxies, each of them fetched from separate Hibernate sessions. 当我比较两个不同的Hibernate代理的值时,发生在JSP页面中,这是一个例外,每个代理都是从单独的Hibernate会话中获取的。

As I described in the question, In first DAO call I got a proxy, then, on second DAO call I got another proxy which is belongs to a different Hibernate sessions. 正如我在问题中所描述的,在第一个DAO调用中,我得到了一个代理,然后,在第二个DAO调用中,我得到了另一个属于不同的Hibernate会话的代理。 Hence the first proxy is a detached hibernate proxy with a "closed session", I got this exception. 因此,第一个代理是具有“关闭的会话”的分离的休眠代理,我遇到了这个异常。

To solve this issue, if you can make try to make your detached proxies, persistent and then compare their values. 要解决此问题,如果可以尝试使分离的代理保持持久,然后比较它们的值。

But I solved this issue by refactoring my code to not to open two different sessions for fetching values from DB. 但是我通过重构代码不打开两个不同的会话来从数据库获取值来解决了这个问题。

Spring should wrap the service with @Transactional annotations with proxy wrapper that will initialize transactions, so you don't do that manually when you don't need to usemore than one transaction in one method. Spring应该使用代理事务包装器使用@Transactional批注来包装服务,这将初始化事务,因此当您不需要在一种方法中使用多个事务时,就不必手动执行此操作。

In XML configuration you need to add <tx-annotation-driven /> in each XML file that declares beans with @Transactional annotations. 在XML配置中,您需要在每个使用@Transactional批注声明bean的XML文件中添加<tx-annotation-driven />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠延迟异常无法初始化代理-无会话 - hibernate lazy exception could not initialize proxy - no Session Hibernate / Spring3:无法初始化代理 - 没有Session - Hibernate/Spring3: could not initialize proxy - no Session Spring + Hibernate异常:没有绑定到线程的hibernate会话 - Spring+Hibernate Exception : No hibernate session bound to thread Hibernate无法初始化代理 - 没有Session - Hibernate could not initialize proxy - no Session Spring DATA JPA + Hibernate-无法初始化代理-修复后无会话: - Spring DATA JPA + Hibernate - could not initialize proxy - no Session after fix: 无法初始化代理-没有会话(春季休眠一对一) - could not initialize proxy - no Session (Spring-Hibernate-one to one) Spring JPA - org.hibernate.LazyInitializationException:无法初始化代理 - 无 Z71AB3B3AE294B3ABDE46 - Spring JPA - org.hibernate.LazyInitializationException: could not initialize proxy - no Session 无法初始化代理-没有会话异常 - could not initialize proxy - no Session Exception Hibernate LazyInitializationException:无法初始化代理-没有会话 - Hibernate LazyInitializationException: could not initialize proxy - no Session Hibernate中的LazyInitializationException:无法初始化代理 - 没有Session - LazyInitializationException in Hibernate : could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM