比较Hibernate中datetime字段的时间部分 [英] Compare time part of datetime field in Hibernate

查看:94
本文介绍了比较Hibernate中datetime字段的时间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序为ORM使用了hibernate(annotations)/ mysql组合。在那个应用程序中,我得到了一个带有日期字段的实体。我正在寻找一种方法来选择时间范围内的那个日期(所以hh:mm:ss没有日期部分)。

在MySQL中有一个函数TIME(表达式),它可以提取时间部分并在where子句中使用它,但是在Hibernate中似乎没有切换时没有切换原生查询。在hibernate中有一个选项可以做到这一点,还是应该循环遍历java中的结果并在那里进行比较?这是否会比MySQL解决方案慢得多,因为它不会使用索引呢?

解决方案

以下函数在HQL中可用,也许你可以使用它们:



second(...),minute(...),hour(...),day (...),月(...),年(...)


I've got an application that uses a hibernate(annotations)/mysql combination for ORM. In that application, I got an entity with a Date field. I'm looking for a way to select on that date within a time range (so hh:mm:ss without the date part).

In MySQL there's a function TIME(expression) that can extract the time part and use that in the where clause, but that does not seem to be available in Hibernate without switching to native queries. Is there an option in hibernate to do this, or should I loop through the results in java and do the comparison there? Would this be much slower as the MySQL solution, since that would not use indexes anyway?

解决方案

The following functions are available in HQL, maybe you could use them:

second(...), minute(...), hour(...), day(...), month(...), year(...)

这篇关于比较Hibernate中datetime字段的时间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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