简体   繁体   中英

How to ignore the Time in a Date Query with JPQL?

I'm having problems doing a query on JPQL using a Date (and ignoring the Time). This is my method:

@Override
public List<Event> findByDayAndStatus(User usr, Date d, char status) {
    Query q = em.createQuery("SELECT e FROM Event as e where " +
                             "e.idusr=:usr AND e.datestart=:d AND " + 
                             "e.status=:status", Event.class);            
    q.setParameter("usr", usr);
    q.setParameter("datestart", d, TemporalType.DATE);
    q.setParameter("status", status);
    return q.getResultList();
}

The problem is I'm getting an empty list and when I omit the date parameter in the query I get my data.

I think is a problem with the date since the way I'm querying is like 2013-03-20 00:00:00 and the database date is like 2013-03-20 12:00:00.

Modify the query as
SELECT e FROM Event as e where e.idusr=:usr AND cast(e.datestart as date) =:d AND e.status=:status It will skip the time from your given date and only return the date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM