Skip to content

Commit dfee51c

Browse files
dreab8beikov
authored andcommitted
HHH-17838 Add test for issue
1 parent c26c446 commit dfee51c

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.onetoone.embeddedid;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Embeddable;
9+
import jakarta.persistence.EmbeddedId;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.FetchType;
12+
import jakarta.persistence.JoinColumn;
13+
import jakarta.persistence.JoinColumns;
14+
import jakarta.persistence.OneToOne;
15+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.JiraKey;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
25+
26+
@DomainModel(
27+
annotatedClasses = {
28+
OneToOneJoinColumnsEmbeddedIdTest.EntityA.class,
29+
OneToOneJoinColumnsEmbeddedIdTest.EntityB.class,
30+
}
31+
)
32+
@SessionFactory
33+
@BytecodeEnhanced(runNotEnhancedAsWell = true)
34+
@JiraKey("HHH-17838")
35+
public class OneToOneJoinColumnsEmbeddedIdTest {
36+
37+
@BeforeEach
38+
public void setUp(SessionFactoryScope scope) {
39+
scope.inTransaction(
40+
session -> {
41+
EntityAKey entityAKey = new EntityAKey( 1, "1" );
42+
EntityA entityA = new EntityA( entityAKey, "te1" );
43+
44+
EntityBKey entityBKey = new EntityBKey( 1, "1" );
45+
EntityB entityB = new EntityB( entityBKey, entityA );
46+
47+
session.persist( entityA );
48+
session.persist( entityB );
49+
}
50+
);
51+
}
52+
53+
@AfterEach
54+
public void tearDown(SessionFactoryScope scope) {
55+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
56+
}
57+
58+
@Test
59+
public void testFind(SessionFactoryScope scope) {
60+
scope.inTransaction(
61+
session -> {
62+
EntityAKey entityAKey = new EntityAKey( 1, "1" );
63+
EntityA entityA = session.find( EntityA.class, entityAKey );
64+
assertThat( entityA ).isNotNull();
65+
66+
EntityB entityB = entityA.getEntityB();
67+
assertThat( entityB ).isNotNull();
68+
69+
EntityBKey key = entityB.getEntityBKey();
70+
assertThat( key.id1 ).isEqualTo( 1 );
71+
assertThat( key.id2 ).isEqualTo( "1" );
72+
}
73+
);
74+
}
75+
76+
@Test
77+
public void testFind2(SessionFactoryScope scope) {
78+
scope.inTransaction(
79+
session -> {
80+
EntityBKey entityBKey = new EntityBKey( 1, "1" );
81+
EntityB entityB = session.find( EntityB.class, entityBKey );
82+
assertThat( entityB ).isNotNull();
83+
84+
EntityA entityA = entityB.getEntityA();
85+
assertThat( entityA ).isNotNull();
86+
87+
EntityAKey entityAKey = entityA.getEntityAKey();
88+
assertThat( entityAKey.id1 ).isEqualTo( 1 );
89+
assertThat( entityAKey.id2 ).isEqualTo( "1" );
90+
91+
assertThat( entityA.getName() ).isEqualTo( "te1" );
92+
}
93+
);
94+
}
95+
96+
@Entity(name = "EntityA")
97+
public static class EntityA {
98+
99+
@EmbeddedId
100+
private EntityAKey entityAKey;
101+
102+
private String name;
103+
104+
@OneToOne(mappedBy = "entityA", fetch = FetchType.LAZY)
105+
private EntityB entityB;
106+
107+
public EntityA() {
108+
}
109+
110+
public EntityA(EntityAKey key, String name) {
111+
this.entityAKey = key;
112+
this.name = name;
113+
}
114+
115+
public EntityAKey getEntityAKey() {
116+
return entityAKey;
117+
}
118+
119+
public String getName() {
120+
return name;
121+
}
122+
123+
public EntityB getEntityB() {
124+
return entityB;
125+
}
126+
}
127+
128+
@Embeddable
129+
public static class EntityAKey {
130+
131+
@Column(name = "id1")
132+
private Integer id1;
133+
134+
@Column(name = "id2")
135+
private String id2;
136+
137+
public EntityAKey() {
138+
}
139+
140+
public EntityAKey(Integer id1, String id2) {
141+
this.id1 = id1;
142+
this.id2 = id2;
143+
}
144+
}
145+
146+
@Entity(name = "EntityB")
147+
public static class EntityB {
148+
@EmbeddedId
149+
private EntityBKey entityBKey;
150+
151+
@OneToOne(optional = false, fetch = FetchType.LAZY)
152+
@JoinColumns({
153+
@JoinColumn(name = "id1", referencedColumnName = "id1", nullable = false,
154+
insertable = false, updatable = false),
155+
@JoinColumn(name = "id2", referencedColumnName = "id2", nullable = false,
156+
insertable = false, updatable = false)
157+
})
158+
private EntityA entityA;
159+
160+
public EntityB() {
161+
}
162+
163+
public EntityB(EntityBKey key, EntityA testEntity) {
164+
this.entityBKey = key;
165+
this.entityA = testEntity;
166+
testEntity.entityB = this;
167+
}
168+
169+
public EntityBKey getEntityBKey() {
170+
return entityBKey;
171+
}
172+
173+
public EntityA getEntityA() {
174+
return entityA;
175+
}
176+
}
177+
178+
@Embeddable
179+
public static class EntityBKey {
180+
181+
@Column(name = "id1")
182+
private Integer id1;
183+
184+
@Column(name = "id2")
185+
private String id2;
186+
187+
public EntityBKey() {
188+
}
189+
190+
public EntityBKey(Integer documentType, String no) {
191+
this.id1 = documentType;
192+
this.id2 = no;
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)