[英]How can I use the sql case clause in HQL?
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 :)如果你能帮忙,请这样做..谢谢:)
I had a similar problem and solution than Stephan .我遇到了与Stephan类似的问题和解决方案。 We use JPA annotations on the getter but the field name was starting with a capital (which is not a good practice anyway).
我们在 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 myTime
but there was only MyTime
.我认为当 Hibernate 根据 getter 解析我的字段名称时,它正在寻找一个名为
myTime
的字段,但只有MyTime
。 The problem was solved when I renamed the field with the correct camel case convention ( myTime
)当我使用正确的驼峰命名约定 (
myTime
) 重命名该字段时,问题就解决了
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
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.
我的错误来自 JPA 期望查询和实体类中的字段名称相同的事实。
@Entity
public class Foo {
private String field1;
...
}
In the query field1
is expected and not Field1
nor FIELD1
and so on ...在查询
field1
是预期的,而不是Field1
或FIELD1
等等......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.