可以使用Spring Data JPA查询方法名称进行逻辑操作符分组吗?

huangapple go评论41阅读模式
英文:

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的情况下,有没有办法实现像这样的查询方法,以在namedescription中搜索相同的文本值?

英文:

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&lt;Thing, Long&gt; {

    public Collection&lt;Thing&gt; findByGroupIDAndNameContainsOrDescriptionContains(Long groupID, String name, String description);

    public Collection&lt;Thing&gt; 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&lt;Thing&gt; findByGroupIdAndNameContainsOrGroupIdAndDescriptionContains(Long groupId1, String name, Long groupId2, String description)

其中 name=descriptiongroupId1=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&lt;Thing&gt; 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&lt;Thing, Long&gt; {
    public Collection&lt;Thing&gt; 
       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&lt;Thing, Long&gt; {
    public Collection&lt;Thing&gt; 
       findByGroupIDAndNameContainsOrGroupIDAndDescriptionContains
           (Long groupId, String name, Long groupId2, String description);
}

huangapple
  • 本文由 发表于 2023年6月6日 03:02:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定