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中编写上述查询?我尝试了很多事情,但没有得到任何解决方案。如果我在 hibernate 中使用 to_char()
,它将给出意外的 token 错误。
最佳答案
您不必使用 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';
关于java - 如何将sql查询写入hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50059187/