Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute query with UNION in hibernate

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!
like image 909
Mahaveer Saini Avatar asked May 31 '17 15:05

Mahaveer Saini


1 Answers

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.

like image 148
Dev Sabby Avatar answered Oct 18 '22 15:10

Dev Sabby