我有以下的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>
现在我想查询所有contents
与相应entries
:
List<Content> contents = session.createQuery("from Content c WHERE c.day IN :days ").setParameterList("days", days).list();
查询返回正确的响应。但是,当我执行以下操作时:
contents.get(0).getEntries()
存在一堆空值。什么是正确加载所有对应的entries
每个content
的正确方法?
我有大约20,000个content
记录,并且大多数记录只有一个条目。
如果我设置lazy="false"
为entries
的列表,我得到Java heap space
错误。
我最终获取entries
和加入contents
:
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>
谢谢。我有大约2万个“内容”记录,大多数记录通常只有一个条目。但现在需要永久加载它们,所以我现在无法分辨它是否可行。 – nickbusted 2015-03-31 08:02:37
我得到以下错误java.lang.OutOfMemoryError:Java堆空间 – nickbusted 2015-03-31 08:05:31
我相信20.000元素太多了。您必须删除lazy = false并调用Hibernate上下文中的contents.get(0).getEntries() – vathek 2015-03-31 08:09:06