使用JPA和Hibernate时DISTINCT如何工作 [英] How does DISTINCT work when using JPA and Hibernate
问题描述
DISTINCT在JPA中使用哪一列,是否可以对其进行更改?
What column does DISTINCT work with in JPA and is it possible to change it?
以下是使用DISTINCT的示例JPA查询:
Here's an example JPA query using DISTINCT:
select DISTINCT c from Customer c
这没有什么意义-区别基于哪个列?是否在Entity上将其指定为注释,因为找不到?
Which doesn't make a lot of sense - what column is the distinct based on? Is it specified on the Entity as an annotation because I couldn't find one?
我想指定进行区分的列,例如:
I would like to specify the column to make the distinction on, something like:
select DISTINCT(c.name) c from Customer c
我正在使用MySQL和Hibernate.
I'm using MySQL and Hibernate.
推荐答案
更新:请参阅投票最多的答案.
我自己的已过时.仅出于历史原因才保留在这里.
在Joins中通常需要在HQL中有区别,而在像您自己这样的简单示例中则不需要.
Distinct in HQL is usually needed in Joins and not in simple examples like your own.
另请参见如何在HQL中创建独立查询
这篇关于使用JPA和Hibernate时DISTINCT如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!