ITeye博客

ERROR LazyInitializationException:42 - failed to lazily initialize a collection

李学力 · 2010-09-29 15:08 · Exception · 企业架构 · JPA · XML · Hibernate · ORM · Web · Markdown

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>

评论(1)

  1. #1 楼 · yoin528 · 2011-01-23 00:20

    懒加载异常你真的觉得不用懒加载就好了么,这样不是对这个设计冲突了么?? 在hibernate 里面web.xml可以配置org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 防止懒加载 在jpa里面可以注解 @PersistenceContext(type=PersistenceContextType.EXTENDED)

    我觉得这种方式更好。

评论功能维护中,当前仅展示历史评论。