Home > Back-end >  How to write a JPA query to find objects between two dates?
How to write a JPA query to find objects between two dates?

Time:02-23

Here's my query

@Query(value = " SELECT * FROM account.statement where `date` between ?1 and ?2")
List<Statement> findAllByDate(String startDate, String endDate);

And this is the error message I get

Caused by: org.hibernate.QueryException: unexpected char: '`' [ SELECT * FROM account.statement where `date` between ?1 and ?2]

The date is the column name. I want to retrieve a list of Statement objects between two dates. The dates in this case are LocalDate objects, like 2000-10-10. I tried using String and a LocalDate type in the parameters, still doesn't work.

I've searched everywhere, on stack overflow and baeldung. I am stuck

CodePudding user response:

List<Statement> findByDateBetween(Date start, Date end);

CodePudding user response:

You should be binding LocalDate, not String, to the placeholders:

@Query(value = " SELECT * FROM account.statement WHERE date BETWEEN ?1 and ?2")
List<Statement> findAllByDate(LocalDate startDate, LocalDate endDate);
  • Related