2017-05-31 1 views
2

ich eine SQL-Abfrage durch Hibernate durchführen, das wie folgt aussieht:Wie Abfrage mit UNION in Hibernate auszuführen

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

Aber es mich

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

Hay Janes, sein Hibernate-Framework in Java, Kategorie bezieht sich auf alle Spalten der Tabelle. –

Antwort

3

Ihre Abfrage ist auf SQL-Ebene in Ordnung, aber im Falle von Hibernate werden Sie diese Ausnahme stellen

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

so wandeln diese Abfrage

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

in zwei Abfragen

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

und rufe sie mit verschiedenen Methoden an.

1

Fehler zeigt, Wie wählen Sie Kategorie ist ihr beliebiges Feld wie das und Sie haben Sie Kategorie Tabelle als Kategorie erwähnt, dann versuchen Sie es entweder andere Ansatz ist in Ordnung * Sie können es auch überprüfen, indem Sie Sternchen als * und Ihr Rest der Abfrage ist korrekt *