spring-data provides a way to generate SQL search by defining the method name.
The following works fine:
@Entity
public class Book {
Date from, to;
}
//CrudRepository<Book>
findByFromDateBetween(Date departure, Date arrival);
But why then does the following not work?
findByFromDateBetweenAndToDateBetween(Date departure, Date arrival);
To connect two date searches, I have to repeat the date:
findByFromDateBetweenAndToDateBetween(Date departure, Date arrival, Date departure, Date arrival);
Question: is it possible to reuse the params?
The Between
keyword naturally binds two parameters. Thus after binding the from clause, the parameter list is exhausted and we don't know which parameters to use for the second criteria.
A manually defined query should do the trick:
interface BookRepository extends Repository<Book, Integer> {
@Query("select b from Book b " +
"where b.from between ?1 and ?2 and b.to between ?1 and ?2")
List<Book> findByDatesBetween(Date departure, Date arrival);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With