开发者

Hibernate find entities not in a collection

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜