如何使用JPA和Spring在列表中查找带有字段的不同行? [英] How to find distinct rows with field in list using JPA and Spring?
问题描述
我使用Spring连接到数据库。我有一个接口扩展 CrudRepository< People,Long>
这是我想要在db上执行的查询: SELECT DISTINCT名称FROM people WHERE name NOT IN UserInputSet
。如果没有 NOT
就没有问题,那么如果有可能的话,我宁愿不做任何sql注释。
有没有办法做到这一点?我查看了spring文档,但找不到任何内容( http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation ) p>
这是我累了,但它不起作用。
@Query(SELECT DISTINCT name FROM people WHERE name NOT IN(?1))
List< String> findNonReferencedNames(List< String>名称);
这是我得到的异常:
创建名为'peopleRepository'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:对于方法的查询验证失败public abstract java.util.List de.test.tasks.persistence.PeopleRepository.findNonReferencedNames(java.util.List)!
和
导致:java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:人没有映射[SELECT name FROM people WHERE name NOT IN(?1)]
解决方案我终于找到了一个简单的解决方案, @ b
列表< People>批注 findDistinctByNameNotIn(List< String>名称);
当然,我得到了人物对象而不是只有字符串。然后我可以在java中进行更改。
I am using Spring to connect to the db. I have an interface extending
CrudRepository<People, Long>
Here is the query I want execute on the db:SELECT DISTINCT name FROM people WHERE name NOT IN UserInputSet
. I would prefer to do it without any sql annotation, so if it is possible without theNOT
that's fine.Is there a way to do it? I looked at the spring doc, but I cannot find anything (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation)
This is what I tired but it is not working.
@Query("SELECT DISTINCT name FROM people WHERE name NOT IN (?1)") List<String> findNonReferencedNames(List<String> names);
this is the exception I get:
Error creating bean with name 'peopleRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List de.test.tasks.persistence.PeopleRepository.findNonReferencedNames(java.util.List)!
and
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: people is not mapped [SELECT name FROM people WHERE name NOT IN (?1)]
解决方案I finally was able to figure out a simple solution without the
@Query
annotation.List<People> findDistinctByNameNotIn(List<String> names);
Of course, I got the people object instead of only Strings. I can then do the change in java.
这篇关于如何使用JPA和Spring在列表中查找带有字段的不同行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!