0

我在构建数据库时遇到了一些问题。我有这两个 hbm 映射:

<class name="br.unicamp.iel.model.Module" table="readinweb_modules">
    <id name="id" type="java.lang.Long">
            <generator class="increment" />
    </id>
    <many-to-one name="course" class="br.unicamp.iel.model.Course"
            column="course_id" fetch="select" />

    <property name="position" type="integer" />
    <property name="module_grammar" type="text" />
</class>

<class name="br.unicamp.iel.model.Course" table="readinweb_courses">
    <id name="id" type="java.lang.Long">
            <generator class="increment" />
    </id>
    <property name="title" length="255" not-null="true" type="string" />

    <property name="idiom" length="255" not-null="true" type="string" />
    <property name="description" type="text" />
    <set name="courseModules" table="readinweb_modules"
            inverse="true" lazy="true" fetch="select">
           <key column="id" not-null="true" />
            <one-to-many class="br.unicamp.iel.model.Module" />
    </set>
</class>

当我尝试访问我的逻辑 bean 上的数据时: List modules = new ArrayList(dao.findById(Course.class, course).getCourseModules());

它给了我一个 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.unicamp.iel.model.Course.courseModules, no session or session was closed

4

1 回答 1

-1

我们需要查看完整的代码

List modules = new ArrayList(dao.findById(Course.class, course).getCourseModules())

您是否在方法内打开和关闭 Session (或 a EntityManagerdao.findById?会话必须仍然打开才能解决惰性关系

于 2014-09-03T01:52:42.583 回答