春天 +休眠延迟抓取

发布于 2024-11-08 04:55:46 字数 3396 浏览 0 评论 0原文

我有 org.hibernate.LazyInitializationException 问题:无法延迟初始化角色集合。

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

这是我的应用程序上下文:

<?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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-11-15 04:55:46

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

解决此问题的常见方法是使用 Spring OpenSessonInViewFilter。这实质上将您的休眠会话范围限制为您的 HTTP 请求。然后,在该 HTTP 请求/响应周期内发生的任何属性访问都将在该会话的范围内。

您可以在 web.xml 中对其进行如下配置:

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

希望这会有所帮助。

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.

流年里的时光 2024-11-15 04:55:46

通常这种类型的问题发生在hibernate中的双向映射中,为此,您在子端(manytoone)上使用@jsonbackrefrence,在父端(onetomany)上使用@jsonmanagementfrence,并且必须添加获取类型EAGER。

generally this type of problem occurs in bidirectional mapping in hibernate ,for this you use @jsonbackrefrence on child side(manytoone) and @jsonmanagedfrence on parent side(onetomany) and must add fetch type EAGER.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文