比方說,我有兩個JPA實體:發現不是從另一個實體引用的所有JPA實體
@Entity
public class EntityA {
@Id
private String id;
@OneToMany
private List<EntityB> b;
...
}
@Entity
public class EntityB {
@Id
private String id;
...
}
我想什麼來完成是定義一個@NamedQuery
檢索不是從引用的所有EntityB
實例任何EntityA
。
天真,我會試圖
@NamedQuery(name = "EntityB.findAllUnassigned", query = "SELECT b FROM EntityB b WHERE NOT b IN (SELECT DISTINCT a.b FROM EntityA a)")
但這最終產生無效的SQL:
select b0_.id as id1_, b0_.attr, ... from b b0_ where b0_.id not in (select distinct . from a a1_, a_b ab2_, b b3_ where a1_.id=ab2_.a_id and ab2_.b_id=b3_.id)
任何的替代品?
你救了我的一天! –