HQL按日期查询查询(Java + NetBeans)

编程入门 行业动态 更新时间:2024-10-27 16:26:22
本文介绍了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)

本文发布于:2023-10-27 02:42:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按日   HQL   NetBeans   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!