如何使用 JPA 和 Spring 在列表中查找具有字段的不同行? [英] How to find distinct rows with field in list using JPA and Spring?

查看:16
本文介绍了如何使用 JPA 和 Spring 在列表中查找具有字段的不同行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 连接到数据库.我有一个接口扩展 CrudRepository 这是我想在数据库上执行的查询:SELECT DISTINCT name FROM people WHERE name NOT IN UserInputSet.我更愿意在没有任何 sql 注释的情况下执行此操作,因此如果可以没有 NOT 也可以.

有办法吗?我查看了 spring 文档,但找不到任何内容(http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation)<小时>

这就是我累但它不起作用.

@Query("SELECT DISTINCT name FROM people WHERE name NOT IN (?1)")列表<字符串>findNonReferencedNames(List names);

这是我得到的例外:

创建名为peopleRepository"的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:查询方法公共抽象 java.util.List de.test.tasks.persistence.PeopleRepository.findNonReferencedNames(java.util.List) 的验证失败!

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: people is not mapping [SELECT name FROM people WHERE name NOT IN (?1)]

解决方案

我终于找到了一个没有 @Query 注释的简单解决方案.

列表<人物>findDistinctByNameNotIn(List names);

当然,我得到的是 people 对象,而不仅仅是 Strings.然后我可以在 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 the NOT 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屋!

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