英文:
Is is possible to write a Spring Data JPA query method name with logical operator grouping?
问题
@Entity
public class Thing {
    private Long groupID;
    private String name;
    private String description;
}
public ThingRepo extends JpaRepository<Thing, Long> {
    public Collection<Thing> findByGroupIDAndNameContainsOrDescriptionContains(Long groupID, String name, String description);
    public Collection<Thing> findByNameContainsOrDescriptionContainsAndGroupID(String name, String description, Long groupID);
}
在不借助JPQL和@Query的情况下,有没有办法实现像这样的查询方法,以在name或description中搜索相同的文本值?
英文:
Let's say I have an entity class like this (minimal code for brevity):
@Entity
public class Thing {
    private Long groupID;
    private String name;
    private String description;
}
Requirement is to implement a search for a String value present in either name or description, for Things with a specific groupID. I'd try writing a Spring Data JPA "derived" query method like one of these:
public ThingRepo extends JpaRepository<Thing, Long> {
    public Collection<Thing> findByGroupIDAndNameContainsOrDescriptionContains(Long groupID, String name, String description);
    public Collection<Thing> findByNameContainsOrDescriptionContainsAndGroupID(String name, String description, Long groupID);
}
To search for the same text in either name or description, I'd pass that same value for both method arguments. However, neither of these produces the expected results, because of how Spring Data processes the logical And and Or operators in the method name - I can't find any way to group the name and description criteria with logical Or.
Is there a way to implement a query method like this without resorting to JPQL and @Query?
答案1
得分: 1
findByGroupIDAndNameContainsOrDescriptionContains(...) 评估为 (A∧B)∨C 但如果我理解正确,您想要 A∧(B∨C),它等价于 (A∧B)∨(A∧C),因此应该是
public Collection<Thing> findByGroupIdAndNameContainsOrGroupIdAndDescriptionContains(Long groupId1, String name, Long groupId2, String description)
其中 name=description 和 groupId1=groupId2。
英文:
findByGroupIDAndNameContainsOrDescriptionContains(...) evaluates to (A∧B)∨C but if I understood correctly you want
A∧(B∨C) which is equivalent to (A∧B)∨(A∧C), so it should be
public Collection<Thing> findByGroupIdAndNameContainsOrGroupIdAndDescriptionContains(Long groupId1, String name, Long groupId2, String description)
where name=description and groupId1=groupId2.
答案2
得分: 0
The precedence order is And then Or, just like Java. You might have to do something along the lines of name and groupid OR description and groupid which means either name and group id matches or description and group id matches.
so, your method could be named:
public ThingRepo extends JpaRepository<Thing, Long> {
    public Collection<Thing> 
       findByGroupIDAndNameContainsOrGroupIDAndDescriptionContains
           (Long groupId, String name, Long groupId2, String description);
}
英文:
The precedence order is And then Or, just like Java. You might have to do something along the lines of name and groupid OR description and groupid which means either name and group id matches or description and group id matches.
so, your method could be named:
public ThingRepo extends JpaRepository<Thing, Long> {
    public Collection<Thing> 
       findByGroupIDAndNameContainsOrGroupIDAndDescriptionContains
           (Long groupId, String name, Long groupId2, String description);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论