【问题标题】:Sakai Hibernate lazy initializeSakai Hibernate 延迟初始化
【发布时间】:2014-08-27 19:55:23
【问题描述】:

我在构建数据库时遇到了一些问题。我有这两个 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 上的数据时: 列出模块 = new ArrayList(dao.findById(Course.class, 当然).getCourseModules());

它给了我一个 org.hibernate.LazyInitializationException:延迟初始化失败 角色集合:br.unicamp.iel.model.Course.courseModules,无 会话或会话已关闭

【问题讨论】:

    标签: sakai


    【解决方案1】:

    我们需要看完整的代码

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

    您是否在dao.findById 方法中打开和关闭会话(或EntityManager)?会话必须仍处于打开状态才能解决惰性关系

    【讨论】:

    • 您真的应该在 cmets 中提出澄清问题,然后在您有解决方案时回答。
    • 好吧,我询问了代码并给了他关于延迟加载工作方式的提示......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    相关资源
    最近更新 更多