ERROR LazyInitializationException:42 - failed to lazily initialize a collection
ERROR LazyInitializationException:42 - failed to lazily initialize a collection of role: com.lixueli.business.model.Person.events, no session or session was closed
在配置文件中加入lazy="false"即可
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lixueli.business.model.Person" table="person">
<id name="id" column="PERSON_ID">
<generator class="native"></generator>
</id>
<property name="age"></property>
<property name="firstName"></property>
<property name="lastName"></property>
<set name="events" table="PERSON_EVENT" lazy="false">
<key column="PERSON_ID"></key>
<many-to-many column="EVENT_ID" class="com.lixueli.business.model.Event"></many-to-many>
</set>
</class>
</hibernate-mapping>
懒加载异常你真的觉得不用懒加载就好了么,这样不是对这个设计冲突了么?? 在hibernate 里面web.xml可以配置org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 防止懒加载 在jpa里面可以注解 @PersistenceContext(type=PersistenceContextType.EXTENDED)
我觉得这种方式更好。