我正在使用Spring连接到数据库。我有一个接口扩展CrudRepository<People, Long>这是我想对执行该数据库查询:SELECT DISTINCT name FROM people WHERE name NOT IN UserInputSet。我更愿意在没有任何sql注释的情况下执行此操作,因此如果没有该注释就可以了NOT。
CrudRepository<People, Long>
SELECT DISTINCT name FROM people WHERE name NOT IN UserInputSet
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)") List<String> findNonReferencedNames(List<String> names);
这是我得到的例外:
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)!
和
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: people is not mapped [SELECT name FROM people WHERE name NOT IN (?1)]
我终于能够找到一个没有@Query注释的简单解决方案。
@Query
List<People> findDistinctByNameNotIn(List<String> names);
当然,我得到了people对象,而不仅仅是Strings。然后,我可以在java中进行更改。