我有两个实体,Item
andData
和一个 DTO 类ItemData
。ItemData
包含Item
并且Data
没有 JPA 映射。为了检索填充的 ItemDatas 列表,我在 JPQL 中使用了构造函数表达式:
select new my.package.ItemData(i, d)
from Item i, Data d
where i.id = d.itemId
这就是 Hibernate 正在做的事情:它不是同时获取 和 的数据Item
,Data
而是先获取它们的 ID,然后在n 个单独的 select 语句中获取数据。有没有办法改变这种行为?
Hibernate:
select
item0_.id as col_0_0_,
data1_.id as col_1_0_
from
ITEM item0_,
DATA data1_
Hibernate:
select
item0_.no as no1_0_,
item0_.description as description1_0_,
item0_.organic as bio1_0_,
item0_.gluten as gluten1_0_,
item0_.laktose as laktose1_0_
from
ITEM item0_
where
item0_.id=?
Hibernate:
select
data0_.amount as amount1_3_0_,
data0_.avg as avg3_0_,
data0_.total as total3_0_
from
DATA data0_
where
data0_.id=?
Hibernate:
select
item0_.no as no1_0_,
item0_.description as description1_0_,
item0_.organic as bio1_0_,
item0_.gluten as gluten1_0_,
item0_.laktose as laktose1_0_
from
ITEM item0_
where
item0_.id=?
Hibernate:
select
data0_.amount as amount1_3_0_,
data0_.avg as avg3_0_,
data0_.total as total3_0_
from
DATA data0_
where
data0_.id=?
...and so on...