日期:2014-05-19  浏览次数:20741 次

关于懒加载的问题,org.hibernate.LazyInitializationException,求教
RT
web.xml中加入
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

两个类的XML如下,一个是Student类,一个是课程类,Course
<hibernate-mapping>
  <class name="com.bzu.entity.Course" table="Course">
  <cache usage="read-write" region="com.bzu.entity.Course"></cache> 
  <id name="id" type="java.lang.Integer">
  <column name="Id" />
  <generator class="native" />
  </id>
  <version name="version" column="version" type="integer"></version>
  <property name="name" type="java.lang.String">
  <column name="Name" length="10" not-null="true" />
  </property>
  <property name="lessonTime" type="java.lang.Integer"/>
  <property name="lessonHour" type="java.lang.Integer"/>
  <property name="lessonPoints" type="java.lang.Integer"/>
  <set name="students" table="stu_course" cascade="save-update" lazy="true">
  <key column="course_id"/>
<many-to-many class="com.bzu.entity.Student" column="stu_id"></many-to-many>  
  </set>
  </class>
</hibernate-mapping>

<hibernate-mapping>
<class name="com.bzu.entity.Student" table="Student">
<cache usage="read-write" region="com.bzu.entity.Student"></cache> 
 
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="native" />
</id>
<version name="version" column="version" type="integer"></version>
<property name="name" type="java.lang.String">
<column name="Name" length="10" not-null="true" />
</property>
<property name="idCard" type="java.lang.String">
<column name="idCard" length="50" />
</property>
<property name="password" type="java.lang.String" />
<property name="sex" type="java.lang.Integer">
<column name="sex" length="30" />
</property>
<property name="birthday" type="java.lang.String">
<column name="birthday" length="10" />
<