简体   繁体   中英

JPQL select Entities assigend/ not assigned to a relation

I have two entities

class A{

}

class B{
    @OneToMany()
@JoinTable(name = "a_b", joinColumns = { @JoinColumn(name = "a_id") }, inverseJoinColumns = { @JoinColumn(name = "b_id") }, uniqueConstraints = { @UniqueConstraint(columnNames = { "b_id" }) })
private Set<A> aSet;
}

The relation is optional so not every A will be assigned to a B and not every B has to have any A(s).

I need two queries one to select all A(s) that are assigned to any B and another to select A(s) that are NOT assigned to any B.

I want to keep the join table and I don't want A bean to reference B so no modification to the relation is allowed.

Any ideas?

You can use RIGHT JOIN :

SELECT a FROM B b RIGHT JOIN b.aSet a WHERE b IS NULL

and

SELECT a FROM B b RIGHT JOIN b.aSet a WHERE b IS NOT NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM