@没有外键约束的实体之间的多通映射。javax.persistence.EntityNotFoundException

@没有外键约束的实体之间的多通映射。javax.persistence.EntityNotFoundException,java,spring-boot,jpa,Java,Spring Boot,Jpa,我有一个实体类证书,它有一个实体类CertificateProfile public class Certificate { @Id private String certId; @ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name="certificateProfileId", foreignKey = @ForeignKey(name = "none")) private CertificateProf

我有一个实体类证书,它有一个实体类CertificateProfile

public class Certificate {
    @Id
    private String certId;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="certificateProfileId", foreignKey = @ForeignKey(name = "none"))
    private CertificateProfile certificateProfile;
}

public class CertificateProfile {
    @Id
    private Long id;

    private String certificateProfileName;
...
}
我正在处理一个遗留数据库。因此,即使certificateProfile列持有certificateProfile的id,也没有外键约束。 当我试图通过
CertificateDataRepository.findAll()
获取证书列表时。我得到
javax.persistence.EntityNotFoundException:找不到id为0的CertificateProfile
我试图将fetchType设置为Lazy,但出现以下错误:

could not initialize proxy [CertificateProfileData#1508963102] - no Session

它发生在一些遗留模式中,这些模式不使用NULL来表示关系列中缺少多对一实体,而是使用了一些神奇的值(我猜在您的例子中,这个神奇的值是0)

默认情况下,如果多对一实体不存在,Hibernate将抛出EntityNotFoundException。您可以使用Hibernate功能
@NotFound
告诉Hibernate忽略此异常,只需将null分配给
多对一
实体引用

@ManyToOne
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name=“certificateProfileId”,foreignKey=@foreignKey(name=“none”))
私人证书档案;
如果数据库级别的两个实体之间没有关系(约束),则不必在类中保留映射。删除以下两行:

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="certificateProfileId", foreignKey = @ForeignKey(name = "none"))
并写如下:

@Column(name="cert_profile_id")
private Long certificateProfileId;

谢谢你的回复。我手动验证了certificateProfile表中存在每个certificateProfile。因此,它并非缺席。