Skip to content

Commit 8786501

Browse files
committed
HHH-18390 Add test for issue
1 parent 816a6ca commit 8786501

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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.BeforeAll;
14+
import org.junit.jupiter.api.Test;
15+
16+
import jakarta.persistence.Column;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.FetchType;
19+
import jakarta.persistence.GeneratedValue;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.JoinColumn;
22+
import jakarta.persistence.JoinColumns;
23+
import jakarta.persistence.MappedSuperclass;
24+
import jakarta.persistence.OneToOne;
25+
import jakarta.persistence.Table;
26+
import jakarta.persistence.UniqueConstraint;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* @author Marco Belladelli
32+
*/
33+
@DomainModel( annotatedClasses = {
34+
OneToOneUniqueJoinColumnsTest.BaseEntity.class,
35+
OneToOneUniqueJoinColumnsTest.EntityA.class,
36+
OneToOneUniqueJoinColumnsTest.EntityB.class,
37+
} )
38+
@SessionFactory
39+
@Jira( "https://hibernate.atlassian.net/browse/HHH-18390" )
40+
public class OneToOneUniqueJoinColumnsTest {
41+
@Test
42+
public void testFindBothEntities(SessionFactoryScope scope) {
43+
scope.inTransaction( session -> {
44+
final EntityA entityA = session.createSelectionQuery( "from EntityA", EntityA.class ).getSingleResult();
45+
assertThat( entityA ).isNotNull().extracting( BaseEntity::getName ).isEqualTo( "entity_a" );
46+
assertThat( entityA.getEntityB() ).isNotNull().extracting( BaseEntity::getName ).isEqualTo( "entity_b" );
47+
} );
48+
}
49+
50+
@Test
51+
public void testFindBothEntitiesInReverse(SessionFactoryScope scope) {
52+
scope.inTransaction( session -> {
53+
final EntityB entityB = session.createSelectionQuery( "from EntityB", EntityB.class ).getSingleResult();
54+
assertThat( entityB ).isNotNull().extracting( BaseEntity::getName ).isEqualTo( "entity_b" );
55+
assertThat( entityB.getEntityA() ).isNotNull().extracting( BaseEntity::getName ).isEqualTo( "entity_a" );
56+
} );
57+
}
58+
59+
@BeforeAll
60+
public void setUp(SessionFactoryScope scope) {
61+
scope.inTransaction( session -> {
62+
final EntityA entityA = new EntityA();
63+
entityA.setName( "entity_a" );
64+
entityA.setColumnAEntityA( 10L );
65+
entityA.setColumnBEntityA( 11L );
66+
session.persist( entityA );
67+
68+
final EntityB entityB = new EntityB();
69+
entityB.setName( "entity_b" );
70+
entityB.setColumnAEntityB( 10L );
71+
entityB.setColumnBEntityB( 11L );
72+
session.persist( entityB );
73+
} );
74+
}
75+
76+
@MappedSuperclass
77+
static class BaseEntity {
78+
@Id
79+
@GeneratedValue
80+
public Long id;
81+
82+
public String name;
83+
84+
public Long getId() {
85+
return id;
86+
}
87+
88+
public String getName() {
89+
return name;
90+
}
91+
92+
public void setName(String name) {
93+
this.name = name;
94+
}
95+
}
96+
97+
@Entity( name = "EntityA" )
98+
@Table( name = "EntityA", uniqueConstraints = @UniqueConstraint( columnNames = {
99+
"columnAEntityA",
100+
"columnBEntityA"
101+
} ) )
102+
static class EntityA extends BaseEntity {
103+
@Column( name = "columnAEntityA" )
104+
public Long columnAEntityA;
105+
106+
@Column( name = "columnBEntityA" )
107+
public Long columnBEntityA;
108+
109+
@OneToOne( mappedBy = "entityA" )
110+
public EntityB entityB;
111+
112+
public Long getColumnAEntityA() {
113+
return columnAEntityA;
114+
}
115+
116+
public void setColumnAEntityA(Long columnAEntityA) {
117+
this.columnAEntityA = columnAEntityA;
118+
}
119+
120+
public Long getColumnBEntityA() {
121+
return columnBEntityA;
122+
}
123+
124+
public void setColumnBEntityA(Long columnBEntityA) {
125+
this.columnBEntityA = columnBEntityA;
126+
}
127+
128+
public EntityB getEntityB() {
129+
return entityB;
130+
}
131+
132+
public void setEntityB(EntityB entityB) {
133+
this.entityB = entityB;
134+
}
135+
}
136+
137+
@Entity( name = "EntityB" )
138+
@Table( name = "EntityB", uniqueConstraints = @UniqueConstraint( columnNames = {
139+
"columnAEntityB",
140+
"columnBEntityB"
141+
} ) )
142+
static class EntityB extends BaseEntity {
143+
@Column( name = "columnAEntityB" )
144+
public Long columnAEntityB;
145+
@Column( name = "columnBEntityB" )
146+
public Long columnBEntityB;
147+
148+
@OneToOne( fetch = FetchType.LAZY )
149+
@JoinColumns( {
150+
@JoinColumn( name = "columnAEntityB", referencedColumnName = "columnAEntityA", insertable = false, updatable = false ),
151+
@JoinColumn( name = "columnBEntityB", referencedColumnName = "columnBEntityA", insertable = false, updatable = false )
152+
} )
153+
public EntityA entityA;
154+
155+
public Long getColumnAEntityB() {
156+
return columnAEntityB;
157+
}
158+
159+
public void setColumnAEntityB(Long columnAEntityB) {
160+
this.columnAEntityB = columnAEntityB;
161+
}
162+
163+
public Long getColumnBEntityB() {
164+
return columnBEntityB;
165+
}
166+
167+
public void setColumnBEntityB(Long columnBEntityB) {
168+
this.columnBEntityB = columnBEntityB;
169+
}
170+
171+
public EntityA getEntityA() {
172+
return entityA;
173+
}
174+
175+
public void setEntityA(EntityA entityA) {
176+
this.entityA = entityA;
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)