Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL SELECT between date statement [closed]

Tags:

java

sql

jpa

jpql

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.

like image 890
Mamadou Avatar asked Mar 18 '11 11:03

Mamadou


People also ask

Can we use subquery in JPQL?

A subselect is a query embedded into another query. It's a powerful feature you probably know from SQL. Unfortunately, JPQL supports it only in the WHERE clause and not in the SELECT or FROM clause. Subqueries can return one or multiple records and can use the aliases defined in the outer query.

Which of the following methods is used to execute a select JPQL query?

Query createQuery(String name) - The createQuery() method of EntityManager interface is used to create an instance of Query interface for executing JPQL statement.

How does @query annotation work?

The @Query annotation gives you full flexibility over the executed statement, and your method name doesn't need to follow any conventions. The only thing you need to do is to define a method in your repository interface, annotate it with @Query, and provide the statement that you want to execute.

Does JPQL support limit?

As stated in the comments, JPQL does not support the LIMIT keyword. You can achieve that using the setMaxResults but if what you want is just a single item, then use the getSingleResult - it throws an exception if no item is found.


2 Answers

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

SELECT e FROM Events e WHERE e.eventsDate BETWEEN :startDate AND :endDate
like image 124
Matt Handy Avatar answered Oct 14 '22 07:10

Matt Handy


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

}
like image 20
Guntash Avatar answered Oct 14 '22 07:10

Guntash