Skip to content

Commit de5c9cd

Browse files
committed
HHH-19479 add test for issue
1 parent b6e5d6f commit de5c9cd

File tree

1 file changed

+129
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)