spring和hibernate 主键表对象,一对多 set,出现session关闭的问题
这个是主键表:
<hibernate-mapping>
<class name="entity.Type" table="type" schema="dbo" catalog="house">//房屋类型
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<set name="houses" inverse="true" lazy="true">
<key>
<column name="typeId" not-null="true" />
</key>
<one-to-many class="entity.House" />//房屋
</set>
</class>
</hibernate-mapping>
在lazy=true 方式下,程序会报错,错误信息是:Error marshalling entity.Type :failed to lazily initialize a collection of role: entity.Type.houses,no session or session was closed.
我查了一下解决办法,最简单的解决办法是把set部分的lazy=true修改为false,但是据说这样会影响效率。
所以,我要请教,在lazy=true的情况下,如何纠正这个错误。
我给出applicationContext.xml的内容:
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://127.0.0.1:1433">
</property>
<property name="username" value="sa"></property>
</bean>
<!--用于大文本-->
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!--用于大文本-->
<property name="lobHandler" ref="defaultLobHandler" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Region.hbm.xml</value>
<value>entity/Type.hbm.xml</value>
<value>entity/Patch.hbm.xml</value>
<value>entity/Picture.hbm.xml</value>
<value>entity/House.hbm.xml</value>
<value>entity/Corporation.hbm.xml</value>
<value>entity/Test.hbm.xml</value></list>
</property></bean>
<!--用于事务-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>