【问题标题】:Spring + hibernate lazy fetchingSpring + hibernate 懒获取
【发布时间】:2011-08-29 01:51:54
【问题描述】:

我有 org.hibernate.LazyInitializationException 的问题:未能延迟初始化角色集合。

gwt + spring + hibernate如何实现懒获取?

这是我的 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>

【问题讨论】:

标签: hibernate spring gwt lazy-evaluation


【解决方案1】:

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

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

您在 web.xml 中进行如下配置:

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

希望这会有所帮助。

【讨论】:

  • 我使用了你的代码,知道我还有另一个问题,也许你可以帮助 om.google.gwt.user.client.rpc.SerializationException: Type 'org.hibernate.collection.PersistentList' was not included在可以通过此 SerializationPolicy 序列化的类型集中或其 Class 对象无法加载。出于安全考虑,此类型不会被序列化。: instance = [] at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:614)
  • 这看起来不像是休眠问题。恐怕我从未使用过GWT。也许一个新问题会给你答案。
  • 绝对是休眠 + GWT 问题。见gwtproject.org/articles/using_gwt_with_hibernate.html
  • 你的 POJO bean 实现了 Serializable 接口吗?
【解决方案2】:

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

【讨论】:

  • 问题与JSON无关。
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 2015-10-25
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 2016-06-19
  • 1970-01-01
相关资源
最近更新 更多