java - Hibernate - 加载子项

标签 java hibernate nhibernate-mapping

我有以下 Hibernate 映射:

<class name="Database.Content" table="..." schema="" catalog="...">
    <id name="id">
        <column name="id" sql-type="int" not-null="true"/>
    </id>
    <property name="week">
        <column name="week" sql-type="int"/>
    </property>
    <property name="day">
        <column name="day" sql-type="int"/>
    </property>
    <property name="hour">
        <column name="hour" sql-type="int"/>
    </property>
    <property name="type">
        <column name="type" sql-type="int" not-null="true"/>
    </property>
    <many-to-one name="group" class="Database.Group">
        <column name="group"/>
    </many-to-one>
    <many-to-one name="table" class="Database.Table">
        <column name="table" not-null="true"/>
    </many-to-one>
    <list name="entries" inverse="true" table="...">
        <key>
            <column name="content" not-null="true"/>
        </key>
        <list-index column="id"/>
        <one-to-many not-found="ignore" class="Database.Entry"/>
    </list>
</class>
<class name="Database.Entry" table="..." schema="" catalog="...">
    <id name="id">
        <column name="id" sql-type="int" not-null="true"/>
    </id>
    <property name="teacher">
        <column name="teacher" sql-type="int" not-null="true"/>
    </property>
    <property name="course">
        <column name="course" sql-type="int" not-null="true"/>
    </property>
    <property name="room">
        <column name="room" sql-type="int" not-null="true"/>
    </property>
    <property name="p">
        <column name="p" sql-type="int" not-null="true"/>
    </property>
    <many-to-one name="content" class="Database.Content" fetch="join" lazy="false">
        <column name="content" not-null="true"/>
    </many-to-one>
</class>

现在我尝试使用相应的条目查询所有内容:

List<Content> contents = session.createQuery("from Content c WHERE c.day IN :days ").setParameterList("days", days).list();

查询返回正确的响应。但是,当我执行以下操作时:

contents.get(0).getEntries()

有一堆空值。为每个内容预先加载所有相应条目的正确方法是什么?

我有大约 20,000 条 content 记录,大多数记录只有一个条目。

如果我为 entries 列表设置 lazy="false",则会收到 Java 堆空间 错误。

<小时/>

我最终获取了条目并加入了内容:

List<Entry> entries = session.createQuery("select e from Entry e Join e.content c WHERE c.day IN :days ").setParameterList("days", days).list();

我还将 lazy 更改为 proxy:

<many-to-one name="content" class="Database.Content" fetch="join" lazy="proxy">
    <column name="content" not-null="true"/>
</many-to-one>

最佳答案

添加lazy=false属性:

 <list name="entries" inverse="true" table="up_timetable_entries" lazy="false">

希望对你有帮助!

关于java - Hibernate - 加载子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29363183/

相关文章:

java - 如何实现无限线程返回数据?

java - Maven - 从多个模块构建一个基于 java/servlet 的 war 文件

java - blob sql 类型的 Hibernate 验证问题

java - Hibernate JPA ManyToOne 组合键

nhibernate - FluentNHibernate 和枚举

java - 你如何在Java中顺序播放音频文件?

java - keytool 和 java keystore.aliases() 之间的 .pfx 文件或 .p12 文件的别名不匹配

java - 当反向引用只是一个 id 属性而不是完整引用时如何执行 OneToMany Hibernate 映射

nhibernate - 在这种情况下,为什么 NHibernate 在 ISession.Refresh 期间抛出 "GenericADOException : could not initialize a collection"异常?

c# - NHibernate:一个基类,几个映射