1

(JPA/Hibernate 问题)我有一个对一个表的查询,我们称之为“Release”,我想从中检索 ONE 行,以映射到一个实体。然后这个实体有五个一对多的关联,这些关联在不久的将来都会用到,所以我想急切地获取它们。现在,这些一对多关联中的每一个都具有介于 3 到 300 之间的行数。如果实际执行,查询总共会加载大约 100 万行。

因此,对广义 HQL 查询进行一些预览(括号中的一些注释):

select rv, al, ... (blabla) from 
from Release rv 
left outer join fetch App app (on releaseId)
left outer join fetch Appconfig ac (on appId)
left outer join fetch Appbind ab (on appId)
left outer join fetch Releaseconfig rc (on releaseId)
left outer join fetch Somestuff st (on releaseId)
left outer join fetch Points p (on releaseId)
left outer join fetch Pointconfig pc (on pointId)
left outer join fetch Pointstuff ps (on pointId)
where rv.releaseId = :id

现在,比如说,App 有 100 行,而 Appconfig 和 Appbind 对于每个 appId 各有 20 行;Releasconfig 大约 15,Somestuff 大约 20,Points 另一个 130,Pointconfig 每个 Point 大约 30,Pointstuff 每个 Point 大约 50。这将导致一个 MASSIVE 查询!

有没有办法让我拆分查询并让 Hibernate 映射它们?说,我首先要求 App、Appconfig 和 Appbind,然后是 Releaseconfig 本身和 Somestuff,然后是 Points、Pointconfig 和 Poinstuff……

谢谢,克里斯

4

1 回答 1

0

您可以通过使用查询预先加载所需的数据来实现这一点,如您所展示的,例如 App、Appconfig 和 Appbind。您没有强制 Hibernate 加载的所有其他集合将由代理表示。

然后,当您需要下一个集合时,您可以使用Hibernate.initialize()传递代理来强制 Hibernate 加载它。这将需要单独的查询。

于 2012-06-28T14:35:21.897 回答