Spring中的数据库会话 [英] Database-sessions in Spring

查看:197
本文介绍了Spring中的数据库会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Spring很新,我想更多地了解会话。我已经使用Hibernate注释过的实体映射了遗留数据库,并构建了一些服务对象来提取,检索等。我的实体还包含其他实体(通过外键映射)和实体集。当我在我的服务中遍历entitiy图时,我没有问题。我的服务被包装在一个TransactionProxyFactoryBean中,所以一切都应该是好的。



因此,在我的View Controller中,我向服务请求了一堆实体。但是当我遍历我的视图控制器中的实体图形(例如显示用户状态的国家名称,州和国家都是懒惰加载的实体)时,Hibernate会话已经关闭,我得到一个LazyInitializationException:无法初始化代理 - 没有会话



我不希望我的服务急于获取数据,因为我的图表很深,甚至可能有循环引用。我也不想明确地触及我将在服务中显示的部分,因为这意味着我必须一遍又一遍地复制/粘贴类似的服务。所以我想要的是当我在视图控制器中时,Hibernate会话仍然打开,并且在视图被提供之前不关闭会话。我可以做些什么来保持它打开?



我的视图bean看起来像这样,是否有任何方法可以将我的TransactionProxyFactoryBean派生的bean注入它?

 < bean name =/class =example.SimpleViewController> 
< property name =userServiceref =userService/>
< property name =catalogServiceref =catalogService/>
< / bean>

干杯



Nik

解决方案

请参阅我对此问题的回答:



我没有使用Spring的Hibernate,所以我不能100%确定Spring能够处理Sessions的能力,但我相信它有一个内置的过滤器和拦截器。处理JPA之外的会话的最佳方式是编写一个过滤器,在将它切换到服务器端其余部分之前打开会话,然后关闭会话。我相信这是Spring内置的东西。


I'm quite new to Spring, and I would like to understand a bit more about sessions. I've mapped a legacy database with Hibernate annotated entities and built a couple of service objects to fetch, retrieve etc. My entities also contain other entities (mapped through foreign keys) and sets of entities. When I traverse the entitiy graph in my services, I have no problem. My services been wrapped in a TransactionProxyFactoryBean, so all should be good.

So, in my View Controller I request a bunch of entities from the services. But when I traverse the entity graph in my view controller (for example to display user's state's country's name, with both state and country being lazily loaded entities), the Hibernate session is already closed and I get a "LazyInitializationException: could not initialize proxy - no Session"

I wouldn't want my services to eagerly fetch the data, that'll consume way to much RAM because my graphs are deep and can even have circular references. I also don't want to explicitly have to touch on the parts that I'll be displaying in the services, as that'd mean that I'll have to copy/paste write similar services over and over again. So what I want is to have the Hibernate session still open when I'm in my view controller and not close down the session until the view has been served. What can I do to keep it open?

My view bean looks like this, is there any way I can inject my TransactionProxyFactoryBean-derived bean into it?

  <bean name="/" class="example.SimpleViewController">
    <property name="userService" ref="userService"/>
    <property name="catalogService" ref="catalogService"/>
  </bean>

Cheers

Nik

解决方案

See my answer to this question: In Spring with jpa/hibernate, how do I keep a session open to avoid lazy initialization exceptions?

I'm using Hibernate without Spring, so I am not 100% sure of the capabilities Spring has to deal with Sessions, but I believe it has both a built in Filter and Interceptor. The best way to handle sessions outside of JPA is to write a filter that opens the session before handing off to the rest of your server side stuff, then closes the session afterwards. I believe this is what Spring's built in stuff does.

这篇关于Spring中的数据库会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆