0

I am using the below method to query using Spring Data JPA.

findByUpdatedGreaterThanAndFromEmailOrToEmailInOrderByUpdatedAsc(final long updated, final String email, final String to);

The query is always passes because of the OR condition.

My intention was findBy(UpdatedGreaterThan)And(FromEmailOrToEmailIn)OrderByUpdatedAsc

Is there anyway to do the same in Spring Data JPA.

Rob
  • 14,746
  • 28
  • 47
  • 65
unnik
  • 1,123
  • 5
  • 18
  • 37
  • Possible duplicate of [Spring data jpa - How to combine multiple And and Or through method name](https://stackoverflow.com/questions/35788856/spring-data-jpa-how-to-combine-multiple-and-and-or-through-method-name) – Jens Schauder Jun 24 '19 at 05:07

1 Answers1

1

You can use the @Query annotation to manually craft a mongodb query to properly enforce the logic of your search criteria.

Something like (I leave the ordering part to you):

@Query(
    "{ $and : [ { 'updated' : { $gt: '?0'} }," + 
               "{ $or : [ { 'email' : '?1' }, { 'to' : '?2' } ] }")
findByUpdatedGreaterThanAndFromEmailOrToEmailInOrderByUpdatedAsc(final long updated, final String email, final String to);

where ?0, ?1, ?2 are placeholders for your findBy method arguments.

Marc Tarin
  • 3,109
  • 17
  • 49
  • Did the same. Was hoping there was some syntax for the method name to do the same. @choda Thank you. – unnik Apr 08 '16 at 00:02