gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:45:33 25 4
gpt4 key购买 nike

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

最佳答案

您的查询在 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")

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

关于java - 如何在 hibernate 中使用 UNION 执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44288641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com