HQL按日期查询查询(Java + NetBeans) [英] HQL make query searching by date (Java+NetBeans)

查看:119
本文介绍了HQL按日期查询查询(Java + NetBeans)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我的MySQL数据库中有一张储备表,日期列定义为DATETIME。我需要使用hibernate进行查询,以便在一天内找到所有储备,无论时间如何,只要它是同一个月份和日期,而我正在这样做

  public List< Reserve> bringAllResByDate(Date date){

em = emf.createEntityManager();
查询q = em.createQuery(SELECT r FROM Reserve r WHERE r.date =:date);
q.setParameter(date,date);

...



我真的没有知道如何使它比较,并给我指定日期的那些,任何帮助?

解决方案

我希望有数据库不可知的方式将数据时间设置为日期。这通常是我如何处理你的情况。

  Calendar from = Calendar.getInstance(); 
Calendar to = Calendar.getInstance();

from.add(Calendar.DATE,-1);
to.add(Calendar.DATE,1);

查询q = em.createQuery(SELECT r FROM Reserve r WHERE r.date>:dateFrom AND r.date<:dateTo);
q.setParameter(dateFrom,from.getTimeInMillis());
q.setParameter(dateTo,to.getTimeInMillis());


Hi all I have the following issue. I have a table of reserves in my MySQL DB, the date columns is defined DATETIME. I need to make a query using hibernate to find all reserves in one day no matter the hour, just that its the same year month and date, and I'm doing this

public List<Reserve> bringAllResByDate(Date date){

em = emf.createEntityManager();
Query q = em.createQuery("SELECT r FROM Reserve r WHERE r.date=:date ");
q.setParameter("date", date);

...

I really dont know how to make it compare, and bring me just those from the specified date, any help??

解决方案

I wish there were a database-agnostic way of casting datetimes as dates. This is usually how I deal with your scenario.

Calendar from = Calendar.getInstance();
Calendar to = Calendar.getInstance();

from.add(Calendar.DATE, -1);
to.add(Calendar.DATE, 1);

Query q = em.createQuery("SELECT r FROM Reserve r WHERE r.date > :dateFrom AND r.date < :dateTo ");
q.setParameter("dateFrom", from.getTimeInMillis());
q.setParameter("dateTo", to.getTimeInMillis());

这篇关于HQL按日期查询查询(Java + NetBeans)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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