简体   繁体   English

n+1 问题与延迟加载一对一关系

[英]n+1 issue in one to one relation with Lazy Loading

My Question is when an null ability occur in one to one relation even my child class Primary Key same like parent class Primary Key So when Insert @PrimaryKeyJoinColumn on one to one relation in Insertion I have seen below Link Issue not-null property references a null or transient value in one to one relation我的问题是,当空能力发生在一对一关系中时,即使我的子类主键与父类主键相同,所以当在插入中插入@PrimaryKeyJoinColumn 一对一关系时,我在下面看到链接问题非空属性引用空或一对一关系中的瞬时值

and when i Remove this tag n+1 issue resolved...so how can i resolved it Please Help当我删除此标签时,n+1 问题已解决...那么我该如何解决请帮忙

private SiteSecurity siteSecurity;
private SiteDetails details;
private SiteAvr avr;
private SiteRectifier rectifier;

@OneToOne( fetch = FetchType.LAZY, mappedBy = "site")
@PrimaryKeyJoinColumn

in Parent Class Fields regarding one to one relations在关于一对一关系的父类字段中

    @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;
}

this is child class so how can i resolved both issue not null and n+1这是子类,所以我如何解决 not null 和 n+1 这两个问题

Simply set optional=true in your OneToOne relationship like:只需在您的OneToOne关系中设置optional=true ,例如:

@OneToOne(fetch = FetchType.LAZY, optional=true)
@PrimaryKeyJoinColumn
public Site getSite() {
    return site;
}

To avoid the n+1 Problem ensure the one to one relationship in sync with the other table if Site Table has a row so another one to one relation table has a row against them and this annotation @PrimaryKeyJoinColumn is on them...为避免 n+1 问题,如果 Site Table 有一行,则确保一对一关系与另一个表同步,因此另一个一对一关系表有一个针对它们的行,并且此注释 @PrimaryKeyJoinColumn 在它们上......

In My case, this strategy will work to avoid the n+1 problem在我的例子中,这个策略可以避免 n+1 问题

Please Got through this Like is also Briefly elaborate the One to One Relation Post请通过这个 Like 也简要说明一对一关系帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM