Java Spring data jpa - 如何通过方法名组合多个And和Or

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35788856/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:07:22  来源:igfitidea点击:

Spring data jpa - How to combine multiple And and Or through method name

javaspring-data-jpaspring-data

提问by Preethi Ramanujam

I am trying to migrate the application. I am working on from Hibernate to Spring Data Jpa.

我正在尝试迁移应用程序。我正在处理从Hibernate 到 Spring Data Jpa 的工作

Though spring data jpa offers simple methods for query building, I am stuck up in creating query method that uses both Andand Or operator.

尽管 spring data jpa 提供了简单的查询构建方法,但我仍然坚持创建同时使用And和 的查询方法Or operator

MethodName - findByPlan_PlanTypeInAndSetupStepIsNullOrStepupStepIs(...)

方法名称 - findByPlan_PlanTypeInAndSetupStepIsNullOrStepupStepIs(...)

When it converts into the query, the first two expressions are combined and it executes as [(exp1 and exp2) or (exp3)].

当它转换为查询时,前两个表达式被组合并作为 执行[(exp1 and exp2) or (exp3)]

whereas required is ](exp1) and (exp2 or exp3)].

而必需的是](exp1) and (exp2 or exp3)]

Can anyone please let me know if this is achievable through Spring data jpa?

任何人都可以让我知道这是否可以通过 Spring data jpa?

回答by fateddy

Option1:You could use named-queries (see Using JPA Named Queries):

选项 1:您可以使用命名查询(请参阅使用 JPA 命名查询):

@Entity
@NamedQuery(name = "User.findByEmailAddress",
  query = "select u from User u where u.emailAddress = ?1")
public class User {

}

public interface UserRepository extends JpaRepository<User, Long> {

   User findByEmailAddress(String emailAddress);
}

Option2: use @Queryto write your custom queries (see Using @Query)

Option2:用于@Query编写自定义查询(请参阅使用 @Query

public interface UserRepository extends JpaRepository<User, Long> {

   @Query("select u from User u where u.emailAddress = ?1")
   User findByEmailAddress(String emailAddress);
}

回答by Oliver Drotbohm

It's currently not possible and also won't be in the future. I'd argue that even if it was possible, with a more complex query you wouldn't want to artificially squeeze all query complexity into the method name. Not only because it becomes hard to digest what's actually going on in the query but also from a client code point of view: you want to use expressive method names, which — in case of a simple findByUsername(…)— the query derivation allows you to create.

目前不可能,将来也不可能。我认为即使有可能,对于更复杂的查询,您也不希望人为地将所有查询复杂性压缩到方法名称中。不仅因为难以消化查询中实际发生的事情,而且从客户端代码的角度来看:您希望使用具有表现力的方法名称,在简单的情况下,findByUsername(…)查询派生允许您创建。

For more complex stuff you' just elevate query complexity into the calling code and it's advisable to rather move to a readable method name that semantically expresses what the query does and keep the query complexity in a manually declared query either using @Query, named queries or the like.

对于更复杂的东西,您只需将查询复杂性提升到调用代码中,建议改为使用可读的方法名称,该名称在语义上表达查询的作用,并使用@Query命名查询等将查询复杂性保留在手动声明的查询中.

回答by Damir Djordjev

Agree with Oliver on long and unreadable method names, but nevertheless and for the sake of argument, you can achieve desired result by using the equivalency

同意 Oliver 对冗长且不可读的方法名称的看法,但是为了论证起见,您可以通过使用等效项来获得所需的结果

A /\ (B \/ C) <=> (A /\ B) \/ (A /\ C)
A and (B or C) <=> (A and B) or (A and C)

So in your case it should look something like this:

所以在你的情况下,它应该是这样的:

findByPlan_PlanTypeInAndSetupStepIsNullOrPlan_PlanTypeInAndStepupStepIs(...)

回答by leonardo rey

Use something like

使用类似的东西

findByFirstElementAndCriteriaOrSecondElementAndCriteria

is like (first & condition) OR ( second & condition) --> condition & ( first or second)

就像 (first & condition) OR ( second & condition) --> condition & ( first or second)