Skip to content

Commit

Permalink
HHH-18399 Add test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed Aug 7, 2024
1 parent ecd22c0 commit 1ab7493
Showing 1 changed file with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.mapping.onetoone;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.OneToOne;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Marco Belladelli
*/
@DomainModel( annotatedClasses = OneToOneJoinTableSelfReferenceTest.Person.class )
@SessionFactory
@Jira( "https://hibernate.atlassian.net/browse/HHH-18399" )
public class OneToOneJoinTableSelfReferenceTest {
@Test
public void testMapping(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final Person parent = new Person();
parent.setId( 2L );

final Person child = new Person();
child.setId( 1L );
parent.setChild( child );

session.persist( child );
session.persist( parent );
} );
scope.inTransaction( session -> {
final Person parent = session.find( Person.class, 2L );
assertThat( parent.getChild() ).isNotNull().extracting( Person::getId ).isEqualTo( 1L );
} );
}

@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
}

@Entity( name = "Person" )
static class Person {
@Id
private Long id;

@OneToOne( fetch = FetchType.LAZY )
@JoinTable(
name = "Parent_Child",
joinColumns = @JoinColumn( name = "parent_id", referencedColumnName = "id" ),
inverseJoinColumns = @JoinColumn( name = "child_id", referencedColumnName = "id" )
)
private Person child;

@OneToOne( mappedBy = "child", fetch = FetchType.LAZY )
private Person parent;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Person getChild() {
return child;
}

public void setChild(Person child) {
this.child = child;
}

public Person getParent() {
return parent;
}
}
}

0 comments on commit 1ab7493

Please sign in to comment.