我有一个实体
@Entity
@Table(name = "`petition`")
@Getter
@Setter
public class Petition extends Auditable {
private static final long serialVersionUID = -3234225397035713824L;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "petition_selected_school", joinColumns = {
@JoinColumn(name = "petition_id", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "school_id",
nullable = false, updatable = false)})
private List<School> selectedSchools;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "petition_basic_school", joinColumns = {
@JoinColumn(name = "petition_id", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "school_id",
nullable = false, updatable = false)})
private List<School> fullBasicSchoolList;
}
我尝试生成像这样的SQL
SELECT
p.id, s.*, b.*
FROM petition p JOIN petition_selected_school s ON p.id = s.petition_id
LEFT JOIN petition_basic_school b ON p.id = b.petition_id AND s.school_id = b.school_id
WHERE s.petition_id = 244
但JPA给我的不一样......
SELECT
petition0_.id, selectedsc1_.*, fullbasics3_.*
FROM "petition" petition0_ INNER JOIN petition_selected_school selectedsc1_ ON petition0_.id = selectedsc1_.petition_id
INNER JOIN "school" school2_ ON selectedsc1_.school_id = school2_.id
LEFT OUTER JOIN petition_basic_school fullbasics3_ ON petition0_.id = fullbasics3_.petition_id
LEFT OUTER JOIN "school" school4_ ON fullbasics3_.school_id = school4_.id AND (school4_.id = school2_.id)
WHERE petition0_.id = 244
我正在尝试构建查询
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Petition> criteriaQuery = builder.createQuery(Petition.class);
Root root = criteriaQuery.from(Petition.class);
ListJoin join = root.joinList("selectedSchools");
ListJoin join2 = root.joinList("fullBasicSchoolList", JoinType.LEFT);
我只想通过inverseJoinColumns进行连接:s.school_id = b.school_id 是否可以使用标准构建器? 然后我想检查fullBasicSchoolList的连接是否为NULL