Spring CrudRepository抛出org.hibernate.LazyInitializationException

时间:2012-07-02 12:47:52

标签: spring spring-data

我使用CrudRepository遇到了问题。示例:我有两个实体,实体A有一个实体B的集合。

class A {
  int id;
  int name;
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  Set<B> bs;
  // getters and setters
}
class B {
  int id;
  int name;
  @ManyToOne(mappedBy="bs")
  A a;
  // getters and setters
}

然后我有2个存储库。

class ARepository extends CrudRepository<A, int>{}
class BRepository extends CrudRepository<B, int>{}

但是当我得到这个时,我得到了一个org.hibernate.LazyInitializationException,我怎么能避免这个?

@Service
@Transactional(readOnly=true)
class ServiceImpl implements Service {
@Resource ARepository ar;

@Override
A a = ar.findOne(int id);
}

这是applicationContext.xml:

<jpa:repositories base-package="com.myproject.repository" />

<context:component-scan base-package="com.myproject.*" />
<context:annotation-config />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="keep-apm" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="database" value="POSTGRESQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
        </bean>
    </property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="username" value="root" />
    <property name="password" value="root" />
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://127.0.0.1:5432/db" />
</bean>
<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory">
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

这是web.xml

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>true</param-value>
    </init-param>

</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>com.myproject.util.LogLocator</listener-class>
</listener>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

a.bs(集合)不会被加载,并且总是抛出 org.hibernate.LazyInitializationException:懒得初始化角色集合:没有会话或会话被关闭

提前谢谢!!

2 个答案:

答案 0 :(得分:2)

您需要使用启动事务,最好是通过Spring的Declarative Transaction Management

在大多数情况下:

  • 在XML中定义TransactionManager
  • 将此添加到您的XML <tx:annotation-driven />
  • 使用@Transactional
  • 注释您的服务方法

您可以在Using @Transactional

部分找到示例设置

答案 1 :(得分:0)

以下是解决方案:

我无法访问我的网络层上的对象,我应该访问服务层中的延迟加载对象,这应该在事务中。