JPQL SELECT between date statement

129,685

Solution 1

Try this query (replace t.eventsDate with e.eventsDate):

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

Solution 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();

}
Share:
129,685
Mamadou
Author by

Mamadou

Updated on September 01, 2020

Comments

  • Mamadou
    Mamadou over 3 years

    I would like to convert this SQL statement to a JPQL equivalent.

    SELECT * FROM events
    WHERE events_date BETWEEN '2011-01-01' AND '2011-03-31';
    

    This correctly retrieves the information from the table events.

    In my Events entity

       @Column(name = "events_date")  
       @Temporal(TemporalType.DATE)  
       private Date eventsDate;
    

    So far this is what I have but it is not working.

    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 ;  
    }
    

    What am I doing wrong? Thanks.

  • Zhenya
    Zhenya about 9 years
    I don't get how this query is different from the query in the question. Also, shouldn't there be <code>:</code> before endDate? And why are are there 2 single quotes after startDate, and after end endDate?