我想将此SQL语句转换为JPQL等效项。
SELECT * FROM events
WHERE events_date BETWEEN '2011-01-01' AND '2011-03-31';
这正确地从表events
中检索信息。
在我的Events
实体
@Column(name = "events_date")
@Temporal(TemporalType.DATE)
private Date eventsDate;
到目前为止,这就是我所拥有的,但它无效。
public List<Events> findAllEvents(Date startDate, Date endDate) {
List<Events> allEvents = entityManager.createQuery(
"SELECT e FROM Events e WHERE t.eventsDate BETWEEN :startDate AND :endDate")
.setParameter("startDate", startDate, TemporalType.DATE)
.setParameter("endDate", endDate, TemporalType.DATE)
.getResultList();
return allEvents ;
}
我做错了什么?感谢。
答案 0 :(得分:73)
尝试此查询(将t.eventsDate
替换为e.eventsDate
):
SELECT e FROM Events e WHERE e.eventsDate BETWEEN :startDate AND :endDate
答案 1 :(得分:3)
public List<Student> findStudentByReports(Date startDate, Date endDate) {
System.out.println("call findStudentMethd******************with this pattern"
+ startDate
+ endDate
+ "*********************************************");
return em
.createQuery(
"' select attendence from Attendence attendence where attendence.admissionDate BETWEEN : startDate '' AND endDate ''"
+ "'")
.setParameter("startDate", startDate, TemporalType.DATE)
.setParameter("endDate", endDate, TemporalType.DATE)
.getResultList();
}