简体   繁体   English

JPQL从POJO中的字符串列表中选择不同的字符串

[英]JPQL to Select Distinct Strings from a List of Strings in a POJO

I have a Java class that has a field which is a list of strings. 我有一个Java类,它的字段是字符串列表。 The elements in the list will vary between objects, but I need to determine a list of distinct values for the field. 列表中的元素在对象之间会有所不同,但是我需要确定该字段的不同值的列表。 The Java class looks something like this: Java类看起来像这样:

public class POJO implements Serializable {

    @ElementCollection
    private List<String> listOfStrings;

}

JPA creates new table called POJO_listOfStrings, and I could run a SQL query like JPA创建一个名为POJO_listOfStrings的新表,我可以运行一个SQL查询,例如

SELECT DISTINCT string FROM dbo.POJO_listOfStrings

to get the information I need. 获得我需要的信息。 I'm at a loss how to do this the correct way with JPQL. 我不知道如何使用JPQL正确地执行此操作。 Things like "SELECT DISTINCT s FROM POJO.listOfStrings s" do not work. 诸如“从POJO.listOfStrings中选择DISTINCT”之类的东西不起作用。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Distinct on an entity object doesn't work on simple select, you need to specify the element in entity which you want to retrieve distinct. 在实体对象上进行区分操作不适用于简单选择,您需要在要检索不同对象的实体中指定元素。

Similar post in below link should give an insight to it. 以下链接中的类似帖子应该对此有所启发。 How do you create a Distinct query in HQL 如何在HQL中创建不同的查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM