如何在hibernate中使用UNION执行查询

时间:2017-05-31 15:18:26

标签: java mysql hibernate spring-boot

我通过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!

2 个答案:

答案 0 :(得分:5)

你的查询在sql级别很好,但是在Hibernate的情况下,你将面临这个异常

3.14

所以转换此查询

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)")

用不同的方法调用它们。

答案 1 :(得分:1)

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