JPQL 在日期语句之间选择 [已关闭]

2022-08-31 20:30:54

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

我做错了什么?谢谢。


答案 1

请尝试此查询(替换为):t.eventsDatee.eventsDate

SELECT e FROM Events e WHERE e.eventsDate BETWEEN :startDate AND :endDate

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

}

推荐