如何在 HQL 中使用 sql case 子句?

发布于 2024-08-06 05:49:24 字数 1679 浏览 6 评论 0原文

关于这个问题我真的需要帮助。

这是代码片段:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡的优雅 2024-08-13 05:49:24

我最近偶然发现了同样的问题。我的错误来自于 JPA 期望实体类中的查询 AND 中具有相同的字段名称。


@Entity
public class Foo {
  private String field1;
  ...
}

在查询中,需要 field1,而不是 Field1FIELD1 等等......

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.


@Entity
public class Foo {
  private String field1;
  ...
}

In the query field1 is expected and not Field1 nor FIELD1 and so on ...

囍孤女 2024-08-13 05:49:24

我遇到了与 Stephan 类似的问题和解决方案。我们在 getter 上使用 JPA 注释,但字段名称以大写字母开头(无论如何,这不是一个好的做法)。

private Timestamp MyTime;
...
@Column(name = "MYTIME")
public Timestamp getMyTime() {
    return MyTime;
}

我认为当 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).

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. The problem was solved when I renamed the field with the correct camel case convention (myTime)

憧憬巴黎街头的黎明 2024-08-13 05:49:24

我相信您忘记了要查询的表。

     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 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文