日期语句之间的JPQL SELECT [英] JPQL SELECT between date statement

查看:133
本文介绍了日期语句之间的JPQL SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此SQL语句转换为等效的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';

这可以正确地从表events中检索信息.

This correctly retrieves the information from the table events.

在我的Events实体

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

我做错了什么?谢谢.

推荐答案

尝试此查询(将t.eventsDate替换为e.eventsDate):

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

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

这篇关于日期语句之间的JPQL SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆