Java 休眠-加载子对象
我有以下Hibernate映射:Java 休眠-加载子对象,java,hibernate,nhibernate-mapping,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 n
<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.get(0).getEntries()
有一堆空值。对于每个内容
,如何正确加载所有相应的条目
我有大约20000条内容
记录,大多数记录只有一个条目
如果我为条目列表设置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=false属性:
<list name="entries" inverse="true" table="up_timetable_entries" lazy="false">
希望对你有帮助 试着打电话:
contents.get(0.getEntries().size()
强制hibernate加载子项。结果总是一个比预期值大得多的数字。我有大约20000条内容
记录,大多数记录通常只有一个条目。但是现在加载它们需要很长时间,所以我无法判断它现在是否工作。我得到以下错误java.lang.OutOfMemoryError:java heap space我认为20000个元素太多了。您必须删除lazy=false并在Hibernate上下文中调用contents.get(0).getEntries(),谢谢,但是我得到了一堆null。我想知道我是否可以得到将两个表连接到Java应用程序的结果,然后查询contents
和entries
<many-to-one name="content" class="Database.Content" fetch="join" lazy="proxy">
<column name="content" not-null="true"/>
</many-to-one>
<list name="entries" inverse="true" table="up_timetable_entries" lazy="false">