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
精彩评论