4

I have a JPA entity that has two attributes to search by "OR" operator instead of "AND". Those are ledgerCode and division:

Company comp = new Company();
ExampleMatcher matcher =
    ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING)
        .withIgnoreCase("name");

comp.setCountryCode(countryCode);
comp.setLedgerCode(ledgerCode);
comp.setDivision(division);
comp.setName(name);

List<Company> result = compSearchRepository.findAll(Example.of(comp, matcher));

Then the SQL result should be (supposing all the parameters not null):

SELECT d 
  FROM COMPANY d 
 WHERE d.countryCode = country 
    OR d.ledgerCode = ledger 
    OR d.division = division 
   AND lower(d.name) LIKE '%name%'

Is it possible? If yes, how so? I couldn't find any code example on Google nor a suggestive method name in ExampleMatcher class.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Robson Braga
  • 323
  • 4
  • 16
  • Possible duplicate of [how to do AND and multiple OR parameters method in spring data JPA](http://stackoverflow.com/questions/26857501/how-to-do-and-and-multiple-or-parameters-method-in-spring-data-jpa) – James Jithin Mar 03 '17 at 18:37

1 Answers1

4

You can't combine a OR and AND search with Query By Example.

With the methode ExampleMatcher.matchingAny() a OR operation is generated instad of a AND operation. But this operation is used for every attribute.

Daniel Käfer
  • 4,458
  • 3
  • 32
  • 41