【问题标题】:How to execute query with UNION in hibernate如何在休眠中使用 UNION 执行查询
【发布时间】:2017-11-01 11:41:17
【问题描述】:

我正在通过 hibernate 执行 SQL 查询,如下所示:

@Query("(select category from Category category where category.isDelete=false and category.status='A' AND " +
        "category.id in (select cat.id from Category cat where cat.isDelete=false and cat.status='A' and cat.parentCategory IS NOT NULL))" +
        "UNION" +
        "(select category from Category category where category.isDelete=false and category.status='A' and category.parentCategory IS NOT NULL)")

但它显示错误

Caused by: java.lang.IllegalArgumentException: node to traverse cannot be null!

【问题讨论】:

  • Hay Janes,java中的hibernate框架,category指表的所有列。

标签: java mysql hibernate spring-boot


【解决方案1】:

您的查询在 sql 级别很好,但在 Hibernate 的情况下,您将面临此异常

Caused by: java.lang.IllegalArgumentException: node to traverse cannot be null!

所以转换这个查询

@Query("(select category from Category category where category.isDelete=false and category.status='A' AND " +
    "category.id in (select cat.id from Category cat where cat.isDelete=false and cat.status='A' and cat.parentCategory IS NOT NULL))" +
    "UNION" +
    "(select category from Category category where category.isDelete=false and category.status='A' and category.parentCategory IS NOT NULL)")

分成两个查询

@Query("select category from Category category where category.isDelete=false and category.status='A' AND " +
    "category.id in (select cat.id from Category cat where cat.isDelete=false and cat.status='A' and cat.parentCategory IS NOT NULL)")

@Query("select category from Category category where category.isDelete=false and category.status='A' and category.parentCategory IS NOT NULL")

并通过不同的方法调用它们。

【讨论】:

    【解决方案2】:

    您如何选择类别是他们的任何字段,并且您已经提到您的类别表作为类别修复它然后尝试其他方法都可以 *您也可以通过将星号作为 * 来检查它并且您的其余查询是正确的*

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多