1

I want a method in jpa repository and it's work like: select from abc.email e where (e.appname='facebook' or e.appname='gmail') and e.status='Pending'

Following is my current method in jpa repository:

List<Email> findByAppnameOrAppnameAndStatus(String appName1, String appName2, String status)

But it's not working as expected. It's work like select from abc.email e where e.appname='facebook' or e.appname='gmail' and e.status='Pending'

How I can add () bracket in jpa repository method?

  • Maybe [this](https://stackoverflow.com/questions/35788856/spring-data-jpa-how-to-combine-multiple-and-and-or-through-method-name) helps – Jens Nov 11 '22 at 22:22
  • @Jens Could you please tell me how my method looks like ? – Akash Phadtare Nov 11 '22 at 22:34
  • I think `List findByAppnameAndStatusOrAppnameAndStatus(String appName1, String status1,String appName2, String status2)` where status 1 and status2 has the same value – Jens Nov 11 '22 at 22:36
  • It works in this case but if the logical expression becomes more complex the extra parameters may increase and the maintainability decrease. – Pierre Demeestere Nov 12 '22 at 11:09

1 Answers1

-1

With JpaRepository queries based on method name you cannot express any bracket setting. The underlying boolean expression will be analyzed from left to right applying the boolean operator precedence. If you need more complexity, you have to define the query with @Query.

  • see https://stackoverflow.com/questions/10287971/spring-jpa-data-or-query – Pierre Demeestere Nov 12 '22 at 10:10
  • Could anyone help me finding what is wrong with this answer ? – Pierre Demeestere Nov 12 '22 at 10:35
  • That is clearly wrong. As you see in the link you provided – Jens Nov 12 '22 at 13:36
  • I think that changing a boolean expression from (A1 or A2) and B to (A1 and B) or (A2 and B) is not a satifying maintenable solution when the corresponding method takes now 4 parameters instead of 3 ? How would we do if the expression was more complex ? In my opinion, changing the expression up to remove the need of brackets (relying only on the precedence rules) shows that you cannot manage the bracket levels with only a method name. – Pierre Demeestere Nov 12 '22 at 16:37