java - 如何编写 sql 查询来休眠
问题描述
id name date
1. ask 2018-04-25 12:30:59
2. msk 2018-04-25 12:40:43
3. sdf 2017-05-25 12:42:34
id=int---->in java id-->int
name=varchar(25)----> in java name-->string
date=datetime------->in java date--->Timestamp
my sql query=select * from table where year(date)='2018';
o/p:1. ask 2018-04-25 12:30:59
2. msk 2018-04-25 12:40:43
select * from table where month(date)='05'
o/p:3. sdf 2017-05-25 12:42:34
please help me i dont know
how to write this query in hibernate
如何在hibernate中编写上述查询?我已经尝试了很多东西,但我没有得到任何解决方案。如果我to_char()
在休眠中使用它会给出一个意外的令牌错误。
解决方案
您不必使用 to_char() 函数。Hibernate 支持年份和日期函数。
请参阅此链接:https ://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html 并查看“14.10。表达式”。
所以你的 hql 看起来就像这样:
select t from table t where YEAR(t.date)='2018';
and select t from table t where MONTH(t.date)='05';
推荐阅读
- javascript - 如何在asp.net mvc视图中使用javascript访问json对象中数组的长度
- c# - 时间 Unity 后禁用脚本
- python - 如何将循环转换为函数
- c# - 部署问题,适用于我朋友的手机,但不适用于我的手机
- mysql - 如何使用 MySQL 合并连续的日期和时间行
- javascript - 如何在Javascript中将对象数组重新映射为对象对象
- java - 如何发布进程 Spring Boot 属性?
- java - 搜索电子邮件的父节点 - 访问子节点的父节点 - Firebase
- android - 使用新设备时 genymotion 设备冻结
- r - 如何使 tidymodels 或工作流结果与 DALEX 解释一起工作?