I have seen the one to one nullability and n+1 Issue Please Help
here is the issue in stack overflow link
For N+1, most likely you are using a bidirectional one-to-one, which, as explained in this article, you can fix with Bytecode Enhancement or by using a unidirectional association.
As for the nullability one, neither your question or the StackOverflow one are clear. You have to provide an example which demonstrates the exact issue you have.
@Entity
@Table(name = "srv_site", catalog = "servicingtables")
public class Site implements java.io.Serializable
{
private Integer id;
//One to One
private SiteSecurity siteSecurity;
private SiteDetails details;
private SiteAvr avr;
private SiteRectifier rectifier;
@OneToOne( fetch = FetchType.LAZY, mappedBy = "site")
@PrimaryKeyJoinColumn
public SiteSecurity getSiteSecurity() {
return siteSecurity;
}
public void setSiteSecurity(SiteSecurity siteSecurity) {
this.siteSecurity = siteSecurity;
}
@OneToOne( fetch = FetchType.LAZY, mappedBy = "site")
@PrimaryKeyJoinColumn
public SiteDetails getDetails() {
return details;
}
public void setDetails(SiteDetails details) {
this.details = details;
}
}
This is Parent Class
@Entity
@Table(name = "srv_site_avr", catalog = "servicingtables")
public class SiteAvr implements java.io.Serializable {
private Integer id;
private Site site;
private Float capacity;
private SrvPosition position;
private AvrBrand brand;
public SiteAvr() {
}
public SiteAvr(Integer id) {
this.id = id;
}
@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 and other one to one relations same like this When i insert the new entry in site class with @PrimaryKeyJoinColumn this annotation on one-to-one relation n+1 solved and uullability issue occur and when i removed this one insertion issue resolved n+1 occurs
child class Primary Key is bind with Parent Class Primary Class
Try with @MapsId
instead of @PrimaryKeyJoinColumn
.
i have seen again this issue not-null property references a null or transient value when i insert the value in Site Class which holds one to one relation
here’s my configuration in Site Class
@Entity
@Table(name = "srv_site", catalog = "servicingtables")
public class Site implements java.io.Serializable
{
private Integer id;
//One to One
private SiteDetails details;
private SiteAvr avr;
private SiteRectifier rectifier;
@OneToOne( fetch = FetchType.LAZY, mappedBy = "site", cascade = CascadeType.ALL ,optional=false)
public SiteDetails getDetails() {
return details;
}
public void setDetails(SiteDetails details) {
this.details = details;
}
@OneToOne( fetch = FetchType.LAZY, mappedBy = "site" ,cascade = CascadeType.ALL ,optional=false)
public SiteAvr getAvr() {
return avr;
}
public void setAvr(SiteAvr avr) {
this.avr = avr;
}
@OneToOne( fetch = FetchType.LAZY, mappedBy = "site", cascade = CascadeType.ALL ,optional=false)
public SiteRectifier getRectifier() {
return rectifier;
}
}
here is the child class its Primary Key is bind with Site Class
@Entity
@Table(name = "srv_site_rectifier", catalog = "servicingtables")
public class SiteRectifier implements java.io.Serializable {
private Integer id;
private Site site;
private RectifierBrand brand;
private String model;
private Integer number;
public SiteRectifier() {
}
public SiteRectifier(Integer id) {
this.id = id;
}
@Id
@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)
@MapsId
@JoinColumn(name = "id")
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
}
Maybe you didn’t synchronize both sides of the association and that’s why you get the exception.
How the Site.id got generated ?
You tell SiteAvr to use the id of Site, but which generator to you specify for Site?