如何在休眠中使用 UNION 执行查询
Posted
技术标签:
【中文标题】如何在休眠中使用 UNION 执行查询【英文标题】:How to execute query with UNION in hibernate 【发布时间】: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指表的所有列。 【参考方案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】:您如何选择类别是他们的任何字段,并且您已经提到您的类别表作为类别修复它然后尝试其他方法都可以 *您也可以通过将星号作为 * 来检查它并且您的其余查询是正确的*
【讨论】:
以上是关于如何在休眠中使用 UNION 执行查询的主要内容,如果未能解决你的问题,请参考以下文章