How to execute query with UNION in hibernate

Mahaveer Saini :

I'm performing a SQL query through hibernate, which looks like:

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

But it showing me error

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

your query is fine at sql level, but in case of Hibernate you will face this exception

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

so convert this query

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

into two queries

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

and call them by different methods.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=469114&siteId=1