我有以下SQL查询,我需要让它在HQL中工作,我尝试了几个解决方案但没有一个工作。(数据库:SQL服务器)
select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and CAST(t.createdTimeStamp AS DATE) = '2016-05-30'
我在HQL中尝试过但没有工作,(createdTimeStamp是sql server中的datetime数据类型)
查询:
select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and CAST(t.createdTimeStamp AS DATE) = '2016-05-30'
错误:
org.hibernate.QueryException: Could not resolve requested type for CAST : DATE
任何有关如何使其发挥作用的建议都会有所帮助。
答案 0 :(得分:5)
你必须按照以下方式编写cast命令,然后才能运行
cast(t.createdTimeStamp as date)
小礼帽中的“演员”,“作为”和“日期”
答案 1 :(得分:2)
我从StackOverflow URL下面找到了答案 StackOverflow-date-is-not-a-recognized-built-in-function-name
我使用CONVERT代替CAST而且它有效
查询:select t.createdTimeStamp FROM ServiceData t , UserInfo C where C.UserId = t.UserId and CONVERT(date,t.createdTimeStamp) = '2016-05-30'
但不确定为什么CAST无法正常工作,因为Hibernate Doc支持CAST