简体   繁体   English

如何在使用JPQL的日期查询中忽略时间?

[英]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). 我在使用日期(忽略时间)对JPQL进行查询时遇到问题。 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. 问题是我得到一个空列表,当我在查询中省略date参数时,我得到了数据。

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. 我认为日期存在问题,因为我的查询方式类似于2013-03-20 00:00:00,而数据库日期类似于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. 从e中选择e FROM事件,其中e.idusr =:usr和cast(e.datestart作为date)=:d AND e.status =:status它将跳过给定日期的时间,仅返回日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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