简体   繁体   中英

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

and when i Remove this tag n+1 issue resolved...so how can i resolved it Please Help

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

Simply set optional=true in your OneToOne relationship like:

@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...

In My case, this strategy will work to avoid the n+1 problem

Please Got through this Like is also Briefly elaborate the One to One Relation Post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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