简体   繁体   中英

Hibernate - load children

I have the following Hibernate Mappings:

<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>

Now I am trying to query all contents with the corresponding entries :

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

The query returns the correct response. However, when I do the following:

contents.get(0).getEntries()

there is a bunch of null values. What is the correct way to eager load all corresponding entries for each content ?

I have about 20,000 content records, and most of the records have only one entry.

If I set lazy="false" for the list of entries , I get Java heap space error.


I ended up fetching entries and joining contents :

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

I also changed lazy to proxy in:

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

Add lazy=false attribute:

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

Hope helped you!

Try calling:

contents.get(0).getEntries().size();

To force hibernate to load the children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM