使用 JPA 和 Hibernate 时 DISTINCT 如何工作 [英] How does DISTINCT work when using JPA and Hibernate

查看:36
本文介绍了使用 JPA 和 Hibernate 时 DISTINCT 如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这没有多大意义 - 不同的列是基于什么列的?是不是因为找不到而在实体上指定为注释?

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.

推荐答案

更新:请查看高票答案.

我自己的现在已经过时了.仅出于历史原因保留在这里.

在连接中通常需要 HQL 中的 Distinct,而不是像您自己的简单示例.

Distinct in HQL is usually needed in Joins and not in simple examples like your own.

另见 如何在 HQL 中创建不同的查询

这篇关于使用 JPA 和 Hibernate 时 DISTINCT 如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆