Java Spring Data Repositories - 在列表中查找 where 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34837725/
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
Spring Data Repositories - Find where field in list
提问by Daniel
I'm trying to use spring PagingAndSortingRepository
with a find MyEntity where field in fieldValues
query as follows:
我正在尝试将 springPagingAndSortingRepository
与find MyEntity where field in fieldValues
查询一起使用,如下所示:
@Repository
public interface MyEntity extends PagingAndSortingRepository<MyEntity, String> {
List<MyEntity> findByMyField(Set<String> myField);
}
But of no success.
但没有成功。
I expected the above function to return all entities whose field matches one of the field values but it only returns empty results.
我希望上述函数返回其字段与字段值之一匹配的所有实体,但它只返回空结果。
Even though it seems like a pretty straight forward ability i could not find any reference to it in the docs.
尽管它看起来是一个非常直接的能力,但我在文档中找不到任何对它的引用。
Is / How that could be achieved?
是/如何实现的?
Thanks.
谢谢。
采纳答案by James Tayler
This should indeed be possible if you are searching on a specific field within your entity and you want to return a list of all that field matches at least one entry in some collection. The documentation heresays this can be achieved using the keyword In
example: findByAgeIn(Collection<Age> ages)
and is equivalent to … where x.age in ?1
如果您在实体中的特定字段上进行搜索,并且想要返回与某个集合中至少一个条目匹配的所有该字段的列表,那么这确实是可能的。此处的文档说这可以使用关键字In
example:来实现findByAgeIn(Collection<Age> ages)
,并且等效于… where x.age in ?1
From your post i'm not 100% sure if this is the use case you are after but give this a try. You will need to search on a specific field, so replace 'field' with whatever field you are searching on. If you are searching on multiple fields it may be possible to concatenate the results with the Or
keyword and specify multiple fields that way.
从您的帖子中,我不能 100% 确定这是否是您所追求的用例,但请尝试一下。您将需要搜索特定字段,因此将“字段”替换为您正在搜索的任何字段。如果您在多个字段上进行搜索,则可以将结果与Or
关键字连接起来并以此方式指定多个字段。
@Repository
public interface MyEntity extends PagingAndSortingRepository<MyEntity, String> {
List<MyEntity> findByFieldIn(Set<String> myField);
}
回答by jvecsei
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/
Your method name has to be findByMyFieldIn
so you got to add an In
at the end to get a where ... in (..)
query.
您的方法名称必须findByMyFieldIn
如此,因此您必须In
在末尾添加一个以获取where ... in (..)
查询。