首页 > 解决方案 > 如何编写 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()在休眠中使用它会给出一个意外的令牌错误。

标签: javahibernate

解决方案


您不必使用 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';

推荐阅读