如何在 HQL 中使用 sql case 子句?
关于这个问题我真的需要帮助。
这是代码片段:
hSql=" select case
when min(start_day_plan) is not NULL then min(start_day_plan)
else to_date((min(insertDate)) - cast('1 month' as interval),'yyyy-MM-dd' )
end
from Project"
getHibernateTemplate().find(hSql);
但这会生成以下错误:
java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.CaseNode
\-[CASE] CaseNode: 'case'
+-[WHEN] SqlNode: 'when'
| +-[IS_NOT_NULL] UnaryLogicOperatorNode: 'is not null'
| | \-[AGGREGATE] AggregateNode: 'min'
| | \-[IDENT] IdentNode: 'start_day_plan' {originalText=start_day_plan}
| \-[AGGREGATE] AggregateNode: 'min'
| \-[IDENT] IdentNode: 'start_day_plan' {originalText=start_day_plan}
\-[ELSE] SqlNode: 'else'
\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'to_date' {originalText=to_date}
\-[EXPR_LIST] SqlNode: 'exprList'
+-[MINUS] BinaryArithmeticOperatorNode: '-' {dataType=org.hibernate.type.DoubleType@bb21ab}
| +-[AGGREGATE] AggregateNode: 'min'
| | \-[IDENT] IdentNode: 'insertDate' {originalText=insertDate}
| \-[METHOD_CALL] MethodNode: '('
| +-[METHOD_NAME] IdentNode: 'cast' {originalText=cast}
| \-[EXPR_LIST] SqlNode: 'exprList'
| +-[QUOTED_STRING] LiteralNode: ''1 month''
| \-[IDENT] IdentNode: 'interval' {originalText=interval}
\-[QUOTED_STRING] LiteralNode: ''yyyy-MM-dd''
正确的查询是什么?我只是想从插入日期中减去 1 个月。
如果您能提供帮助,请这样做..谢谢:)
I really need help regarding this one.
Here is the code snippet:
hSql=" select case
when min(start_day_plan) is not NULL then min(start_day_plan)
else to_date((min(insertDate)) - cast('1 month' as interval),'yyyy-MM-dd' )
end
from Project"
getHibernateTemplate().find(hSql);
But this generates the error below:
java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.CaseNode
\-[CASE] CaseNode: 'case'
+-[WHEN] SqlNode: 'when'
| +-[IS_NOT_NULL] UnaryLogicOperatorNode: 'is not null'
| | \-[AGGREGATE] AggregateNode: 'min'
| | \-[IDENT] IdentNode: 'start_day_plan' {originalText=start_day_plan}
| \-[AGGREGATE] AggregateNode: 'min'
| \-[IDENT] IdentNode: 'start_day_plan' {originalText=start_day_plan}
\-[ELSE] SqlNode: 'else'
\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'to_date' {originalText=to_date}
\-[EXPR_LIST] SqlNode: 'exprList'
+-[MINUS] BinaryArithmeticOperatorNode: '-' {dataType=org.hibernate.type.DoubleType@bb21ab}
| +-[AGGREGATE] AggregateNode: 'min'
| | \-[IDENT] IdentNode: 'insertDate' {originalText=insertDate}
| \-[METHOD_CALL] MethodNode: '('
| +-[METHOD_NAME] IdentNode: 'cast' {originalText=cast}
| \-[EXPR_LIST] SqlNode: 'exprList'
| +-[QUOTED_STRING] LiteralNode: ''1 month''
| \-[IDENT] IdentNode: 'interval' {originalText=interval}
\-[QUOTED_STRING] LiteralNode: ''yyyy-MM-dd''
what will be the correct query for this? im just trying to subtract 1 month from the insertdate.
If you could help, please do so..thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近偶然发现了同样的问题。我的错误来自于 JPA 期望实体类中的查询 AND 中具有相同的字段名称。
在查询中,需要
field1
,而不是Field1
或FIELD1
等等......I stumble upon this same problem recently. My error came from the fact JPA expects the same fields name in the query AND in the entity class.
In the query
field1
is expected and notField1
norFIELD1
and so on ...我遇到了与 Stephan 类似的问题和解决方案。我们在 getter 上使用 JPA 注释,但字段名称以大写字母开头(无论如何,这不是一个好的做法)。
我认为当 Hibernate 根据 getter 解析我的字段名称时,它正在寻找名为
myTime
的字段,但只有MyTime
。当我使用正确的驼峰式大小写约定重命名该字段时,问题得到了解决(myTime
)I had a similar problem and solution than Stephan. We use JPA annotations on the getter but the field name was starting with a capital (which is not a good practice anyway).
I think when Hibernate was resolving my field name based on the getter it was looking for a field named
myTime
but there was onlyMyTime
. The problem was solved when I renamed the field with the correct camel case convention (myTime
)我相信您忘记了要查询的表。
I believe your forgot the table you want to query.