开发者

Hibernate find entities not in a collection

开发者 https://www.devze.com 2023-02-19 13:27 出处:网络
I have an entity CategoryModel. One of the properties is copied below: @OneToMany(cascade = CascadeType.ALL)

I have an entity CategoryModel. One of the properties is copied below:

@OneToMany(cascade = CascadeType.ALL)
private List<CategoryModel> children;

How can I perform a query that will return all CategoryModel entities that are not in another CategoryMod开发者_JAVA百科el entity's children collection (i.e. the root categories)?


The most efficient way would be to make relationship bidirectional and apply IS NULL at the "many" side:

SELECT c FROM CategoryModel c WHERE c.parent IS NULL

If you can't change relationship, you can do something like this:

SELECT c FROM CategoryModel p RIGHT JOIN p.children c WHERE p IS NULL
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号