52

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.

Matt
  • 74,352
  • 26
  • 153
  • 180
Mamadou
  • 577
  • 1
  • 4
  • 7

2 Answers2

98

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

SELECT e FROM Events e WHERE e.eventsDate BETWEEN :startDate AND :endDate
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
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();

}
Guntash
  • 51
  • 1
  • 1
    I don't get how this query is different from the query in the question. Also, shouldn't there be : before endDate? And why are are there 2 single quotes after startDate, and after end endDate? – Zhenya Apr 01 '15 at 08:32