我的问题是,当一对一关系中出现 null 能力时,即使我的子类主键与父类主键相同,所以当在插入中的一对一关系上插入 @PrimaryKeyJoinColumn 时,我在下面看到了链接问题 not-null property references a null or transient value in one to one relation
当我删除此标签时,n+1 问题已解决...那么我该如何解决它请帮助
private SiteSecurity siteSecurity;
private SiteDetails details;
private SiteAvr avr;
private SiteRectifier rectifier;
@OneToOne( fetch = FetchType.LAZY, mappedBy = "site")
@PrimaryKeyJoinColumn
在父类字段中关于一对一关系
@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "site"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
这是子类,所以我如何解决 not null 和 n+1 的问题
最佳答案
只需在 OneToOne
关系中设置 optional=true
即可,例如:
@OneToOne(fetch = FetchType.LAZY, optional=true)
@PrimaryKeyJoinColumn
public Site getSite() {
return site;
}
关于java - 与延迟加载一对一关系的 n+1 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358064/