java 如何在 HQL 中使用 sql case 子句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1502536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use the sql case clause in HQL?
提问by side_dish
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.
什么是正确的查询?我只是想从插入日期中减去 1 个月。
If you could help, please do so..thanks :)
如果你能帮忙,请这样做..谢谢:)
回答by KLE
I believe your forgot the table you want to query.
我相信您忘记了要查询的表。
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 MyTable
回答by Stephan
I stumble upon this same problem recently. My error came from the fact JPA expects the same fields name in the query ANDin the entity class.
我最近偶然发现了同样的问题。我的错误来自 JPA 期望查询和实体类中的字段名称相同的事实。
@Entity
public class Foo {
private String field1;
...
}
In the query field1is expected and not Field1nor FIELD1and so on ...
在查询中field1是预期的,不是Field1也不是FIELD1等等......
回答by seberenimer
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).
我遇到了与Stephan类似的问题和解决方案。我们在 getter 上使用 JPA 注释,但字段名称以大写开头(无论如何这不是一个好习惯)。
private Timestamp MyTime;
...
@Column(name = "MYTIME")
public Timestamp getMyTime() {
return MyTime;
}
I think when Hibernate was resolving my field name based on the getter it was looking for a field named myTimebut there was only MyTime. The problem was solved when I renamed the field with the correct camel case convention (myTime)
我认为当 Hibernate 根据 getter 解析我的字段名称时,它正在寻找一个名为的字段,myTime但只有MyTime. 当我使用正确的驼峰命名规则 ( myTime)

