I have a JPA entity TimeSlot with a LocalDateTime
field called startDateTime
:
@Entity
public class TimeSlot {
private LocalDateTime startDateTime;
...
}
I am using Hibernate on WildFly 10.1. How do I query all entities with the startDateTime
between startDate
and endDate
?
private List<TimeSlot> getTimeSlotsByStartDateEndDate(LocalDate startDate, LocalDate endDate) {
return entityManager.createNamedQuery("TimeSlot.findByStartDateEndDate", TimeSlot.class)
.setParameter("startDate", startDate)
.setParameter("endDate", endDate).getResultList());
}
This query fails because a timestamp is not a date:
@NamedQueries({
@NamedQuery(name = "TimeSlot.findByStartDateEndDate",
query = "select t from TimeSlot t" +
// fails because a timestamp is not a date
" where t.startDateTime between :startDate and :endDate"),
})
You must convert LocalDateTime and LocalDate to java.sql.Timestamp then add your converter classes to the persistent.xml file then everything must be ok. For LocalDateTimeConverter :
For LocalDateTime:
Lastly, add your classes to the persistent.xml