I have a spring boot app and as database Im using Oracle and, I want to filter the returning result by start date and end date, I used this query
@Query(
value = "select * from student s inner join usuario u on s.email=u.email where u.usuario = ?#{principal.username} and ((trunc(s.date_creation) >= ?1 AND ?2 is null) or (trunc(s.date_creation) >= ?1 AND trunc(s.date_creation) <= ?2))",
nativeQuery = true
)
Page<Student> findByCurrentStudentByPeriod(LocalDate startDate, Optional<LocalDate> endDate, Pageable pageable);
but I got this error
Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected DATE got BINARY
this answer gives the explanation https://stackoverflow.com/a/16592812/14994239, and https://stackoverflow.com/a/12320806/14994239 suggests to use Case statement
I made it with Case statement like below but is not working
select * from student s inner join usuario u on s.email=u.email where u.usuario = ?#{principal.username} and (Case when ?2 is null then trunc(s.date_creation) >= ?1 else (trunc(s.date_creation) >= ?1 AND trunc(s.date_creation) <= ?2) end)
Here is my controller
@GetMapping("/studentCourant/dateCreation")
public ResponseEntity<List<Student>> getAllStudentByPeriod(
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@DateTimeFormat(pattern = "yyyy-MM-dd") Optional<LocalDate> endDate,
@org.springdoc.api.annotations.ParameterObject Pageable pageable
) {
log.debug("REST request to get a page of StudentCourant by dateCreation");
Page<Student> page = studentRepository.findByCurrentStudentByPeriod(
startDate,
endDate,
pageable
);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
Any help would appreciated, Thank you!