我有一个实体类证书,其中有一个实体类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;
...
}
我正在使用旧版数据库。因此,即使certificateProfileId列包含certificateProfile的ID,也没有外键约束。
当我尝试通过CertificateDataRepository.findAll()
获取证书列表时。我得到javax.persistence.EntityNotFoundException: Unable to find CertificateProfile with id 0
我试图将fetchType设置为Lazy,但出现此错误:
could not initialize proxy [CertificateProfileData#1508963102] - no Session
答案 0 :(得分:1)
它发生在某些传统模式中,这些模式不使用NULL表示关系列中不存在多对一实体,而是使用一些不可思议的值(我猜在您的情况下,该不可思议的值为0)。
默认情况下,如果多对一实体不存在,则Hibernate会抛出AssemblyInfo
。您可以使用Hibernate功能EntityNotFoundException
来告诉Hibernate忽略此异常,只需将null分配给@NotFound
实体引用。
many-to-one
答案 1 :(得分:0)
如果在数据库级别的2个实体之间没有关系(约束),则不必将映射保留在类中。删除以下两行:
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="certificateProfileId", foreignKey = @ForeignKey(name = "none"))
并编写如下:
@Column(name="cert_profile_id")
private Long certificateProfileId;