Spring+Hibernate 配置一对多关联,及session关闭问题处理
Hibernate一对多关联映射时,出现failed to lazily initialize a collection of role: XXXXXX, no session or session was closed异常的解决方案:
一、配置一对多映射关联
<set name="quAn" order-by="AN_CODE asc" lazy="true">
<key column="AN_QU_ID" />
<one-to-many ref="sessionFactory" />
</bean>
<!-- 用拦截器方式 处理事务 -->
<bean id="transactionInterceptor"
ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
注: <value>*Dao</value>为需要进行事务处理的bean名称,可以使用模糊匹配