Spring + Hibernate惰性抓取 [英] Spring + hibernate lazy fetching

查看:60
本文介绍了Spring + Hibernate惰性抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何使用gwt + spring + hibernate实现延迟读取?

如何使用gwt + spring + hibernate实现延迟读取? / p>

这是我的appContext:

 <?xml version =1.0 encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beansxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns:aop = http://www.springframework.org/schema/aopxmlns:context =http://www.springframework.org/schema/contextxmlns:tx =http://www.springframework.org/schema/ txxsi:schemaLocation =http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org / schema / context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/ tx / spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd\">
< bean id =hibernateTemplateclass =org.springframework.orm.hibernate3.HibernateTemplate>
< property name =sessionFactory>
< ref local =sessionFactory/>
< / property>
< / bean>
< context:annotation-config />
< context:component-scan base-package =com.yeah.server。*/>
< aop:aspectj-autoproxy />
<! - Mysql数据库连接信息 - >
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql://192.168.1.4:3306 / YeaH/>
< property name =usernamevalue =root/>
< property name =passwordvalue =/>
< / bean>
<! - Hibernate - >
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =mappingResources>
< list>
< value> com / yeah / shared / model / User.hbm.xml< / value>
< value> com / yeah / shared / model / Comment.hbm.xml< / value>
< value> com / yeah / shared / model / Album.hbm.xml< / value>
< value> com / yeah / shared / model / Picture.hbm.xml< / value>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto> update< / prop>
< /道具>
< / property>
< / bean>
<! - HIbernate会话管理
< bean名称=openSessionInViewInterceptor
class =org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor>
< property name =sessionFactoryref =sessionFactory/>
< property name =singleSessionvalue =false/>
< / bean>
- >
<! - 一个PlatformTransactionManager仍然是必需的 - >
< bean id =transactionManagerclass =org.springframework.jdbc.datasource.DataSourceTransactionManager>
< property name =dataSourceref =dataSource/>
< / bean>
< / beans>


解决方案

发生异常是因为您尝试访问在加载hibernate会话后,域对象上的延迟加载属性已关闭。



解决此问题的常用方法是使用Spring OpenSessonInViewFilter。这基本上将你的hibernate会话范围扩展到你的HTTP请求。然后,在该HTTP请求/响应周期内发生的任何属性访问都将在该会话的范围内。



您可以在web.xml中配置它,如下所示:

 < filter> 
< filter-name> Spring OpenSessionInViewFilter< / filter-name>
< filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
< / filter-class>
< / filter>

希望这有助于您。


I have problem with org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role.

How to implement lazy fetching with gwt + spring + hibernate?

Here's my appContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="   http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
    <context:annotation-config/>
    <context:component-scan base-package="com.yeah.server.*"/>
    <aop:aspectj-autoproxy/>
    <!--Mysql database connection info-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.1.4:3306/YeaH"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <!-- Hibernate -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <value>com/yeah/shared/model/User.hbm.xml</value>
                <value>com/yeah/shared/model/Comment.hbm.xml</value>
                <value>com/yeah/shared/model/Album.hbm.xml</value>
                <value>com/yeah/shared/model/Picture.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!--HIbernate session management
 <bean name="openSessionInViewInterceptor"
     class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
 <property name="sessionFactory" ref="sessionFactory"/>
 <property name="singleSession" value="false"/>
 </bean>
 -->
    <!-- a PlatformTransactionManager is still required -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

解决方案

The exception occurs because you're trying to access a lazy loaded property on a domain object after the hibernate session it was loaded in has been closed.

A common way of fixing this is to use the Spring OpenSessonInViewFilter. This essentially scopes your hibernate session to your HTTP request. Then any property access that occurs within that HTTP request/response cycle will be within the scope of that session.

You configure it in your web.xml as follows:

<filter>
    <filter-name>Spring OpenSessionInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
</filter>

Hope this helps.

这篇关于Spring + Hibernate惰性抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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