# One to one N+1 and nullability Issue

**URL:** <https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383>\
**Category:** Hibernate ORM\
**Created:** [March 19, 2018, 10:51am UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383 "2018-03-19T10:51:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahtesham\_Asif](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ahtesham_asif/32/1261_2.png) [@Ahtesham\_Asif](https://discourse.hibernate.org/u/Ahtesham_Asif)\
**Post date:** [March 19, 2018, 10:51am UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/1 "2018-03-19T10:51:50Z")

</div>

I have seen the one to one nullability and n+1 Issue Please Help  
here is the issue in stack overflow link

[N+1](https://stackoverflow.com/questions/49358064/n1-select-issue-in-one-to-one-relation)

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 19, 2018, 11:45am UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/2 "2018-03-19T11:45:22Z")

</div>

For N+1, most likely you are using a bidirectional one-to-one, which, as explained in [this article](https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/), 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.

---

<div class="post-metadata">

**Author:** ![Ahtesham\_Asif](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ahtesham_asif/32/1261_2.png) [@Ahtesham\_Asif](https://discourse.hibernate.org/u/Ahtesham_Asif)\
**Post date:** [March 19, 2018, 12:03pm UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/3 "2018-03-19T12:03:58Z")

</div>

```auto
@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

```

```auto
@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

---

<div class="post-metadata">

**Author:** ![Ahtesham\_Asif](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ahtesham_asif/32/1261_2.png) [@Ahtesham\_Asif](https://discourse.hibernate.org/u/Ahtesham_Asif)\
**Post date:** [March 19, 2018, 12:05pm UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/4 "2018-03-19T12:05:00Z")

</div>

child class Primary Key is bind with Parent Class Primary Class

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 19, 2018, 12:19pm UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/5 "2018-03-19T12:19:06Z")

</div>

Try with `@MapsId` instead of `@PrimaryKeyJoinColumn`.

---

<div class="post-metadata">

**Author:** ![Ahtesham\_Asif](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/ahtesham_asif/32/1261_2.png) [@Ahtesham\_Asif](https://discourse.hibernate.org/u/Ahtesham_Asif)\
**Post date:** [March 20, 2018, 7:31am UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/6 "2018-03-20T07:31:57Z")

</div>

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

```auto
@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

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

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 20, 2018, 9:09am UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/7 "2018-03-20T09:09:40Z")

</div>

Maybe you didn’t synchronize both sides of the association and that’s why you get the exception.

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [May 30, 2018, 5:41am UTC](https://discourse.hibernate.org/t/one-to-one-n-1-and-nullability-issue/383/8 "2018-05-30T05:41:28Z")

</div>

How the Site.id got generated ?  
You tell SiteAvr to use the id of Site, but which generator to you specify for Site?
