Select 类别,它不是 HQL 中的 parent

Select category which is not a parent in HQL

我有一个 table 类别,它在 MYSQL 数据库中具有如下所示的字段和值。

id  name   parent    sort_order
1   Men    null       0
2   Women  null       1
3   shirt   1         0
4   salwar  2         1

我想编写一个 HQL 查询来获取不属于任何其他类别的 parent 的所有类别。我不能使用 parent is not null 选择,因为我有更多的级别。当另一个级别到来时,这里的衬衫和萨尔瓦可以 parent。我该怎么做?

试试这个 hql

select c1 from category c1 
where c1.id not in 
(select c2.parent_id from category c2 where c2.parent_id = c1.id)