【发布时间】:2014-07-16 22:48:24
【问题描述】:
我需要从查询中获取数据,这检索实体 A、实体 B 和实体 C 的 id 也树自定义字段。像这样的
SELECT
a.id_entityA,
b.id_entityB,
c.id_entityC,
sum(some) * sum(another),
avg(of_some) * any_factor,
another_operations,
From ... Many Selects, joins, etc
这样检索信息
54 | 80 | 60 | 5421 | 56474.4 | 4540
在休眠中,我创建一个 SQLQuery 来获取 DTO 中的信息
public class ExampleDTO {
private Integer idA;
private Integer idB;
private Integer idC;
private Number fieldX;
private Number fieldY;
private Number fieldZ;
//getters and settters
}
在查询执行中我添加了 resultTransformer
.setResultTransformer(Transformers.aliasToBean(ExampleDTO.class))
并且有效!!!
但是,我需要实体 A、B 和 C 的另一个字段,
public class ExampleDTO {
// bottom of fields
private EntityA entityA;
private EntityB entityB;
private EntityC entityC;
// news and olds getters and setters
所以我在一个小圆领里读到这个来填补
for(ExampleDTO e : list)
{
e.setEntityA(entityADao.getById(e.getIdA());
e.setEntityB(entityBDao.getById(e.getIdB());
e.setEntityC(entityCDao.getById(e.getIdC());
}
有效,但其获取数据的老式低性能方式。
有一些神奇的方法可以在同一个查询中获取三个实体,只需要运行第一个查询并执行 NOT bluce 来填充我的 dto 列表
编辑
假设EntityA、EntityB 和EntityC 用@Entity 标注
【问题讨论】:
-
如果我正确理解您要执行的操作,您希望运行一个查询来获取三个实体,并运行 SQL 命令来执行数学运算,所有这些都在一个查询中完成。上次我使用 Hibernate 时,我相信不可能在 HQL 或 Criteria/getById() 中执行任意 SQL 公式;也就是说,您必须选择是运行 SQL,还是让 Hibernate 发挥其 ORM 的魔力,但不能同时做到这两点。但这可能不再准确......
标签: java mysql performance hibernate