OneToOne self referencing entity with JoinTable

Hi there!
Given the following entity:

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

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 (?,?,?)]

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 with a test case template that reproduces the issue.

It is reproducible in 6.5.2.
I created the following ticket: [HHH-18399] - Hibernate JIRA

1 Like

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:

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:

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

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