# OneToOne self referencing entity with JoinTable

**URL:** <https://discourse.hibernate.org/t/onetoone-self-referencing-entity-with-jointable/9961>\
**Category:** Hibernate ORM\
**Created:** [July 17, 2024, 11:56am UTC](https://discourse.hibernate.org/t/onetoone-self-referencing-entity-with-jointable/9961 "2024-07-17T11:56:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuJulDo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mujuldo/32/3359_2.png) [@MuJulDo](https://discourse.hibernate.org/u/MuJulDo)\
**Post date:** [July 17, 2024, 11:56am UTC](https://discourse.hibernate.org/t/onetoone-self-referencing-entity-with-jointable/9961/1 "2024-07-17T11:56:34Z")

</div>

Hi there!  
Given the following entity:

```auto
@Entity
public class Person implements Serializable {

	@Id
	@NaturalId
	@Column(columnDefinition = "DECIMAL(39,0)")
	private BigInteger id;

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(
			name = "Parent_Child",
			joinColumns = {
					@JoinColumn(name = "parent_id", 
						referencedColumnName = "id", 
						nullable = false, 
						unique = true, 
						foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
			},
			inverseJoinColumns = {
					@JoinColumn(name = "child_id", 
						referencedColumnName = "id", 
						nullable = false, 
						unique = true, 
						foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
			}
	)
	private Person child;

	@JsonIgnore
	@OneToOne(cascade = CascadeType.REFRESH, mappedBy = "child")
	private Person parent;
}

```

When Hibernate tries to persist a Person entity, with another Person entity set as its’ child, it fails while trying to insert a record in the join table, because it uses the same parameter twice:

```auto
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement [No value specified for parameter 3] 
[/* insert for com.person.Person */insert into Parent_Child (child_id,parent_id,parent_id) values (?,?,?)]

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [July 17, 2024, 1:56pm UTC](https://discourse.hibernate.org/t/onetoone-self-referencing-entity-with-jointable/9961/2 "2024-07-17T13:56:03Z")

</div>

Are you sure you updated Hibernate to the latest version 6.5.2/6.6.0? If so, and you still have the problem, please create an issue in our [issue tracker](https://hibernate.atlassian.net) with a [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces the issue.

---

<div class="post-metadata">

**Author:** ![MuJulDo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mujuldo/32/3359_2.png) [@MuJulDo](https://discourse.hibernate.org/u/MuJulDo)\
**Post date:** [July 17, 2024, 3:23pm UTC](https://discourse.hibernate.org/t/onetoone-self-referencing-entity-with-jointable/9961/3 "2024-07-17T15:23:25Z")

</div>

It is reproducible in 6.5.2.  
I created the following ticket: [[HHH-18399] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-18399)

---

<div class="post-metadata">

**Author:** ![MuJulDo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mujuldo/32/3359_2.png) [@MuJulDo](https://discourse.hibernate.org/u/MuJulDo)\
**Post date:** [July 22, 2024, 8:31am UTC](https://discourse.hibernate.org/t/onetoone-self-referencing-entity-with-jointable/9961/4 "2024-07-22T08:31:35Z")

</div>

**A bit of context:**

When having a **parent** entity persisted in the DB with a **child** entity set on it, we want to be able to either retrieve the **parent** with its’ linked **child** , OR retrieve the **child** with its’ linked **parent**. In order to achieve this we opted to use a OneToOne self-referencing relationship, with a join table.

This was working fine for the following hibernate and spring versions:

```auto
hibernate-core version: 5.6.7.Final
spring-boot version: 2.6.6

```

Recently we decided to upgrade spring and hibernate to the following versions:

```auto
hibernate-core version: 6.5.2.Final
spring-boot version: 3.2.2

```

With the new versions, we started encountering the issue mentioned above.
