0

I want to have a filter query using spring data JPA. condition is something like this:

(A or B or C) and D

when I want to use Spring Data JPA I should change it to be like below (The precedence order is And then Or, just like Java.):

(A and D) or (B and D) or (C and D)

the code in the Spring Repository class is:

Page<SystemEntity>  findByNameContainsAndFlagIsFalseOrFamilyContainsAndFlagIsFalseOrEmailContainsAndFlagIsFalse(String name, String family ,String email, Pageable pageable); 

you can see that the D condition at the above line "AndFlagIsFalse" is repeated 3 times.

is there any way to implement using a shorter format like "(A or B or C) and D"?

SM. Hosseini
  • 171
  • 1
  • 13

1 Answers1

1

Using the @Query annotation you can define the query directly:

@Query("SELECT se FROM SystemEntity se WHERE (se.name = :name AND se.flag = true) OR (. . .) OR (. . .)")
Page<SystemEntity> findSystemEntities(String name, String family ,String email, Pageable pageable);
Bill Mair
  • 1,073
  • 6
  • 15
  • thank you bill Mair, I'm looking for a shorter format like "(A or B or C) and D" which is more easy and readable – SM. Hosseini Feb 13 '23 at 11:51
  • Then your problem doesn't appear to be Java or JPA, you have a appear to have problems with boolean algebra. – Bill Mair Feb 13 '23 at 14:42