日期声明之间的JPQL SELECT

时间:2011-03-18 11:01:11

标签: java sql jpa jpql

我想将此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 ;  
}

我做错了什么?感谢。

2 个答案:

答案 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();

}