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

Preethi Ramanujam picture Preethi Ramanujam · Mar 4, 2016 · Viewed 39.3k times · Source

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

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

MethodName - findByPlan_PlanTypeInAndSetupStepIsNullOrStepupStepIs(...)

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

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

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

Answer

Damir Djordjev picture Damir Djordjev · Mar 15, 2017

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

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(...)