Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在新dto查询中按计算列排序_Hibernate_Jpa_Entitymanager - Fatal编程技术网

Hibernate 如何在新dto查询中按计算列排序

Hibernate 如何在新dto查询中按计算列排序,hibernate,jpa,entitymanager,Hibernate,Jpa,Entitymanager,我有一个疑问: em.createQuery("select new SomeDto(some.name, " + "(select max(other.weight) from Other other where other.someId = some.id) as otherWeight" + ") from Some some order by otherWeight") .getResultList(); 这不起作用,因为hibernate忽略了作为其他权重,而只是将作为列1\u 0生

我有一个疑问:

em.createQuery("select new SomeDto(some.name, " +
"(select max(other.weight) from Other other where other.someId = some.id) as otherWeight" +
") from Some some order by otherWeight")
.getResultList();
这不起作用,因为hibernate忽略了
作为其他权重
,而只是将
作为列1\u 0
生成

这种
selectnewdto
query有没有办法获取列的别名?
如果没有:我如何进行这样的查询(将查询结果映射到DTO构造函数)?

您可以尝试以下查询,这也应该更快:

select new SomeDto(some.name, max(other.weight))
from Other other right outer join other.some some
group by some.id, some.name
order by max(other.weight)

如果您还没有关联,则需要将多对一(或一对一)关联从
Other
引入
Some

如果每个
Some
都有一个
Other
的话,这是可行的,但如果这是可选的,我会排除这些结果(内部连接)对吗?您可能是对的。我最初的目标是以某种方式在“orderby”子句中使用别名,以使整个内容更具可读性(想象一下,与
max(other.weight)
相比,这里有一个更复杂的表达式),但是,如果您经常手工编写查询,这似乎是“纯sql”中更需要的。在构造这样的jpql查询时,可以将这样的表达式放入变量中并以这种方式连接起来,以保持其可读性。因此,我认为对于我想要的东西并没有一个真正的解决方案,因为它不是一个人“应该想要的”。