使用Hibernate在OneToOne映射中始终保持左联接



我有两个实体PersonBlacklistInfo,它们之间有OneToOne关系。

Person.java

@Data
@Entity
@Immutable
@Table(name = "person_tbl")
public class Person {
@Column(name = "person_id")
@Id
private Long personId;

// ... other members
@OneToOne
@JoinColumn(name = "person_id", referencedColumnName = "person_id")
private BlacklistInfo blacklistInfo;
}

AdditionalInfo.java

@Data
@Entity
@Immutable
@Table(name = "blacklist_info_tbl")
public class BlacklistInfo {
@Id
@Column(name = "person_id")
private Long personId;

// ... other members
}

CCD_ 5的CCD_ 4可以为空。当前我想查找未列入黑名单的人员

我可以通过以下操作之一来完成操作:

  1. 查询所有人并从他们中删除黑名单
  2. 做左联接查询,检查BlacklistInfopersonId为空
    select p from Person p left join p.blacklistInfo where p.blacklistInfo.personId is null
    

问题是:Can I do the 2nd operation without explicitly mentioning left join the in query section? Can it be done by modifying entity relationship ?

我使用hibernate-5.4.12.Finalspring-boot-2.2.x

您还想如何查询这些信息?使用JPQL/HQL来做这件事是完全可以的。

最新更新