[英]n+1 issue in one to one relation with Lazy Loading
我的問題是,當空能力發生在一對一關系中時,即使我的子類主鍵與父類主鍵相同,所以當在插入中插入@PrimaryKeyJoinColumn 一對一關系時,我在下面看到鏈接問題非空屬性引用空或一對一關系中的瞬時值
當我刪除此標簽時,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;
}
為避免 n+1 問題,如果 Site Table 有一行,則確保一對一關系與另一個表同步,因此另一個一對一關系表有一個針對它們的行,並且此注釋 @PrimaryKeyJoinColumn 在它們上......
在我的例子中,這個策略可以避免 n+1 問題
請通過這個 Like 也簡要說明一對一關系帖子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.