|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.mapping.onetoone; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.Jira; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.CascadeType; |
| 17 | +import jakarta.persistence.Entity; |
| 18 | +import jakarta.persistence.FetchType; |
| 19 | +import jakarta.persistence.Id; |
| 20 | +import jakarta.persistence.JoinColumn; |
| 21 | +import jakarta.persistence.JoinTable; |
| 22 | +import jakarta.persistence.OneToOne; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Marco Belladelli |
| 28 | + */ |
| 29 | +@DomainModel( annotatedClasses = OneToOneJoinTableSelfReferenceTest.Person.class ) |
| 30 | +@SessionFactory |
| 31 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18399" ) |
| 32 | +public class OneToOneJoinTableSelfReferenceTest { |
| 33 | +@Test |
| 34 | +public void testMapping(SessionFactoryScope scope) { |
| 35 | +scope.inTransaction( session -> { |
| 36 | +final Person parent = new Person(); |
| 37 | +parent.setId( 2L ); |
| 38 | + |
| 39 | +final Person child = new Person(); |
| 40 | +child.setId( 1L ); |
| 41 | +parent.setChild( child ); |
| 42 | + |
| 43 | +session.persist( child ); |
| 44 | +session.persist( parent ); |
| 45 | +} ); |
| 46 | +scope.inTransaction( session -> { |
| 47 | +final Person parent = session.find( Person.class, 2L ); |
| 48 | +assertThat( parent.getChild() ).isNotNull().extracting( Person::getId ).isEqualTo( 1L ); |
| 49 | +} ); |
| 50 | +} |
| 51 | + |
| 52 | +@AfterAll |
| 53 | +public void tearDown(SessionFactoryScope scope) { |
| 54 | +scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 55 | +} |
| 56 | + |
| 57 | +@Entity( name = "Person" ) |
| 58 | +static class Person { |
| 59 | +@Id |
| 60 | +private Long id; |
| 61 | + |
| 62 | +@OneToOne( fetch = FetchType.LAZY ) |
| 63 | +@JoinTable( |
| 64 | +name = "Parent_Child", |
| 65 | +joinColumns = @JoinColumn( name = "parent_id", referencedColumnName = "id" ), |
| 66 | +inverseJoinColumns = @JoinColumn( name = "child_id", referencedColumnName = "id" ) |
| 67 | +) |
| 68 | +private Person child; |
| 69 | + |
| 70 | +@OneToOne( mappedBy = "child", fetch = FetchType.LAZY ) |
| 71 | +private Person parent; |
| 72 | + |
| 73 | +public Long getId() { |
| 74 | +return id; |
| 75 | +} |
| 76 | + |
| 77 | +public void setId(Long id) { |
| 78 | +this.id = id; |
| 79 | +} |
| 80 | + |
| 81 | +public Person getChild() { |
| 82 | +return child; |
| 83 | +} |
| 84 | + |
| 85 | +public void setChild(Person child) { |
| 86 | +this.child = child; |
| 87 | +} |
| 88 | + |
| 89 | +public Person getParent() { |
| 90 | +return parent; |
| 91 | +} |
| 92 | +} |
| 93 | +} |
0 commit comments