Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do AND and multiple OR parameters method in spring data JPA

I am trying to formulate a method name for this query :

  @Query("from Employees where department = ?1 and (fullTime = true or contractor = true or subContractor = true)")

I thought this method will do the trick, but it does an and on dept and full time

  public List<Employees> findByDepartmentAndfullTimeTrueOrContractorTrueOrSubContractorTrue(String dept);

This is a related question : Spring JPA Data "OR" query but was asked in 2012. Is there a way to achieve this without having to use @Query ?

like image 447
happybuddha Avatar asked Nov 11 '14 04:11

happybuddha


1 Answers

This is currently not supported and probably never will be for a very simple reason:

Derived queries are considered a means to define very simple queries. I admit this is blurry but if you get to findByDepartmentAndfullTimeTrueOrContractorTrueOrSubContractorTrue it's time to rethink whether that's actually what you want to expose to clients. It's awkward to write, awkward to read and probably actually more than a collection of predicates but conveying a higher-level meaning and thus should be named in amore descriptive way.

The solution - as you already discovered - is to use @Query or Querydsl predicates.

like image 183
Oliver Drotbohm Avatar answered Sep 24 '22 19:09

Oliver Drotbohm