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

Mah*_*ini 6 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)")
Run Code Online (Sandbox Code Playgroud)

但它显示我的错误

Caused by: java.lang.IllegalArgumentException: node to traverse cannot be null!
Run Code Online (Sandbox Code Playgroud)

Dev*_*bby 5

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

Caused by: java.lang.IllegalArgumentException: node to traverse cannot be null!
Run Code Online (Sandbox Code Playgroud)

所以转换这个查询

@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)")
Run Code Online (Sandbox Code Playgroud)

分成两个问题

@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")
Run Code Online (Sandbox Code Playgroud)

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