Skip to content

Commit 3e6456a

Browse files
committed
HHH-19479 add test for issue
1 parent 4df8388 commit 3e6456a

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.annotations.refcolnames.constraints;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.JoinColumn;
11+
import jakarta.persistence.ManyToOne;
12+
import jakarta.persistence.RollbackException;
13+
import org.hibernate.exception.ConstraintViolationException;
14+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
15+
import org.hibernate.testing.orm.junit.JiraKey;
16+
import org.hibernate.testing.orm.junit.Jpa;
17+
import org.junit.jupiter.api.Test;
18+
19+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
20+
import static org.junit.jupiter.api.Assertions.fail;
21+
22+
@Jpa(annotatedClasses = {ManyToOneRefColumnNameTest.This.class, ManyToOneRefColumnNameTest.That.class})
23+
@JiraKey("HHH-19479")
24+
class ManyToOneRefColumnNameTest {
25+
26+
@Test void test(EntityManagerFactoryScope scope) {
27+
scope.getEntityManagerFactory().getSchemaManager().truncate();
28+
This thisThing = new This();
29+
That thatThing = new That();
30+
thatThing.compositeKeyOne = 1;
31+
thatThing.compositeKeyTwo = 2;
32+
thatThing.singleKey = "hello";
33+
thisThing.thatBySingleKey = thatThing;
34+
thisThing.thatByCompositeKey = thatThing;
35+
scope.inTransaction( em -> {
36+
em.persist( thatThing );
37+
em.persist( thisThing );
38+
} );
39+
}
40+
41+
@Test void testUnique(EntityManagerFactoryScope scope) {
42+
scope.getEntityManagerFactory().getSchemaManager().truncate();
43+
This thisThing = new This();
44+
That thatThing = new That();
45+
thatThing.compositeKeyOne = 1;
46+
thatThing.compositeKeyTwo = 2;
47+
thatThing.singleKey = "hello";
48+
thisThing.thatBySingleKey = thatThing;
49+
thisThing.thatByCompositeKey = thatThing;
50+
scope.inTransaction( em -> {
51+
em.persist( thatThing );
52+
em.persist( thisThing );
53+
} );
54+
That thing = new That();
55+
try {
56+
thing.singleKey = "hello";
57+
scope.inTransaction( em -> {
58+
em.persist( thing );
59+
} );
60+
fail();
61+
}
62+
catch (RollbackException re) {
63+
assertInstanceOf( ConstraintViolationException.class, re.getCause() );
64+
}
65+
}
66+
67+
@Test void testUniqueKey(EntityManagerFactoryScope scope) {
68+
scope.getEntityManagerFactory().getSchemaManager().truncate();
69+
This thisThing = new This();
70+
That thatThing = new That();
71+
thatThing.compositeKeyOne = 1;
72+
thatThing.compositeKeyTwo = 2;
73+
thatThing.singleKey = "hello";
74+
thisThing.thatBySingleKey = thatThing;
75+
thisThing.thatByCompositeKey = thatThing;
76+
scope.inTransaction( em -> {
77+
em.persist( thatThing );
78+
em.persist( thisThing );
79+
} );
80+
That thing = new That();
81+
thing.singleKey = "goodbye";
82+
thing.compositeKeyOne = 1;
83+
thing.compositeKeyTwo = 2;
84+
try {
85+
scope.inTransaction( em -> {
86+
em.persist( thing );
87+
} );
88+
fail();
89+
}
90+
catch (RollbackException re) {
91+
assertInstanceOf( ConstraintViolationException.class, re.getCause() );
92+
}
93+
}
94+
95+
@Entity
96+
static class That {
97+
@Id
98+
long id;
99+
100+
@Column(nullable = false)
101+
String singleKey;
102+
103+
int compositeKeyOne;
104+
int compositeKeyTwo;
105+
}
106+
107+
@Entity
108+
class This {
109+
@Id
110+
long id;
111+
112+
@JoinColumn(referencedColumnName = "singleKey")
113+
@ManyToOne
114+
That thatBySingleKey;
115+
116+
@JoinColumn(referencedColumnName = "compositeKeyOne")
117+
@JoinColumn(referencedColumnName = "compositeKeyTwo")
118+
@ManyToOne
119+
That thatByCompositeKey;
120+
}
121+
}

0 commit comments

Comments
 (0)