Java Spring Data JPA Distinct - 从单列返回结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38224925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:06:42  来源:igfitidea点击:

Spring Data JPA Distinct - Return results from a single column

javajpaspring-dataspring-data-jpa

提问by Rasmus Sj?rslev

I have some data that contains a STATEfield (String/Text) that defines what state a given request is currently in (e.g. pending, approved denied etc.) and to get all the unique values from that column I can run the following TSQL query

我有一些数据包含一个STATE字段(字符串/文本),该字段定义了给定请求当前处于什么状态(例如,待处理、已批准被拒绝等)并从该列中获取所有唯一值,我可以运行以下 TSQL 查询

SELECT DISTINCT STATE FROM CALLOUT_REQUEST

SELECT DISTINCT STATE FROM CALLOUT_REQUEST

where CALLOUT_REQUESTis my table name and STATEbeing the field which returns something like:

CALLOUT_REQUEST我的表名在哪里,是STATE返回如下内容的字段:

STATE

状态

  • approved
  • denied
  • pending
  • ...
  • 得到正式认可的
  • 拒绝
  • 待办的
  • ...

However I don't understand how I would turn that into a query in my repository as it seems I need a "by" statement or some other filter mechanism which i can get the STATEbased on?

但是我不明白如何将其转换为我的存储库中的查询,因为似乎我需要一个“by”语句或其他一些过滤机制,我可以获得STATE基于?

What I am looking to return - as shown in the raw TSQL query above - is some kind of List or Array object which contains all the unique/distinct values in all of the STATEfields.

我希望返回的 - 如上面的原始 TSQL 查询所示 - 是某种 List 或 Array 对象,其中包含所有STATE字段中的所有唯一/不同值。

So in pseudo code i think i am looking for something like this:

所以在伪代码中,我想我正在寻找这样的东西:

String[] states = repository.findDisinctState();

String[] states = repository.findDisinctState();

where findDistinctState()would then return an array of sorts.

findDistinctState()然后where会返回一个数组。

Hope that makes sense - I am very new to Java and Spring in general so I think I am missing some conceptual knowledge to utilise the above.

希望这是有道理的 - 总的来说,我对 Java 和 Spring 非常陌生,所以我认为我缺少一些概念性知识来利用上述内容。

UPDATE:

更新:

The 'state' concept is closed so i could implement that as an enum - only problem is i dont know how to do that :) Ill look into how i can do that as i think it fits perfectly with what i am trying to achieve.

“状态”概念已关闭,因此我可以将其实现为枚举 - 唯一的问题是我不知道如何做到这一点:) 我会研究如何做到这一点,因为我认为它完全符合我想要实现的目标。

The List i get from the query provided is intended to be used to get a count of all the occurrences. I had this code before to get a total count for each of the 'states':

我从提供的查询中获得的列表旨在用于获取所有出现次数的计数。我之前有这个代码来获得每个“状态”的总数:

Map stats = new HashMap();
String[] states = {"approved", "denied", "pending", "deactivated"};
for (int i = 0; i < states.length; i++) {
    stats.put(states[i], repository.countByState(states[i]));
}

Am i correct in understanding that the states Arraythat i have in the above code snippet could be turned into an enum and then i dont even need the custom @Queryanymore?

我是否正确理解Array我在上述代码片段中的状态可以变成枚举,然后我什至@Query不再需要自定义?

采纳答案by Luís Soares

If that stateconcept is closed and you know it's a possible set of values, it should be an enum.

如果该state概念是封闭的并且您知道它是一组可能的值,则它应该是一个枚举。

After that you can create queriesthat you invoke like:

之后,您可以创建您调用的查询,例如:

repository.findByState(State.APPROVED)

If you can't create an enum, you need a separate method to get the distinct values, which can't be provided by JPA, because you need an list of strings and not a list of CalloutRequests. Then you need to specify a query manually like:

如果无法创建枚举,则需要一个单独的方法来获取不同的值,而 JPA 无法提供这些值,因为您需要的是字符串列表而不是CalloutRequests列表。然后你需要手动指定一个查询,如:

@Query("SELECT DISTINCT State FROM CALLOUT_REQUEST")
List<String> findDistinctStates();

回答by meskobalazs

You can use a JPQL query for this, with the @org.springframework.data.jpa.repository.Queryannotation:

您可以为此使用 JPQL 查询,并带有@org.springframework.data.jpa.repository.Query注释:

@Query("select distinct state from CalloutRequest")
List<String> findDistinctStates();