Skip to content

Commit 0990682

Browse files
committed
HHH-19480 add tests
1 parent c8ef3d0 commit 0990682

File tree

4 files changed

+162
-3
lines changed

4 files changed

+162
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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.OneToMany;
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 java.util.HashSet;
21+
import java.util.Set;
22+
23+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
import static org.junit.jupiter.api.Assertions.fail;
26+
27+
@Jpa(annotatedClasses = {OneToManyRefColumnNameTest.This.class, OneToManyRefColumnNameTest.That.class})
28+
@JiraKey("HHH-19480")
29+
class OneToManyRefColumnNameTest {
30+
31+
@Test void test(EntityManagerFactoryScope scope) {
32+
scope.getEntityManagerFactory().getSchemaManager().truncate();
33+
This thisThing = new This();
34+
That thatThing = new That();
35+
thatThing.compositeKeyOne = 1;
36+
thatThing.compositeKeyTwo = 2;
37+
thatThing.singleKey = "hello";
38+
thatThing.theseOnSingleKey.add( thisThing );
39+
thatThing.theseOnCompositeKey.add( thisThing );
40+
scope.inTransaction( em -> {
41+
em.persist( thatThing );
42+
em.persist( thisThing );
43+
} );
44+
That thing = new That();
45+
thing.singleKey = "goodbye";
46+
thing.compositeKeyOne = 5;
47+
thing.compositeKeyTwo = 3;
48+
scope.inTransaction( em -> {
49+
em.persist( thing );
50+
} );
51+
}
52+
53+
@Test void testForeignKey(EntityManagerFactoryScope scope) {
54+
scope.getEntityManagerFactory().getSchemaManager().truncate();
55+
This thisThing = new This();
56+
That thatThing = new That();
57+
thatThing.compositeKeyOne = 1;
58+
thatThing.compositeKeyTwo = 2;
59+
thatThing.singleKey = "hello";
60+
thatThing.theseOnSingleKey.add( thisThing );
61+
thatThing.theseOnCompositeKey.add( thisThing );
62+
scope.inTransaction( em -> {
63+
em.persist( thatThing );
64+
em.persist( thisThing );
65+
} );
66+
67+
assertThrows( ConstraintViolationException.class, () ->
68+
scope.inTransaction( em -> {
69+
em.createQuery( "update OneToManyRefColumnNameTest$That set singleKey = 'goodbye'" ).executeUpdate();
70+
} )
71+
);
72+
73+
assertThrows( ConstraintViolationException.class, () ->
74+
scope.inTransaction( em -> {
75+
em.createQuery( "update OneToManyRefColumnNameTest$That set compositeKeyOne = 69" ).executeUpdate();
76+
} )
77+
);
78+
}
79+
80+
@Test void testUnique(EntityManagerFactoryScope scope) {
81+
scope.getEntityManagerFactory().getSchemaManager().truncate();
82+
This thisThing = new This();
83+
That thatThing = new That();
84+
thatThing.compositeKeyOne = 1;
85+
thatThing.compositeKeyTwo = 2;
86+
thatThing.singleKey = "hello";
87+
thatThing.theseOnSingleKey.add( thisThing );
88+
thatThing.theseOnCompositeKey.add( thisThing );
89+
scope.inTransaction( em -> {
90+
em.persist( thatThing );
91+
em.persist( thisThing );
92+
} );
93+
That thing = new That();
94+
try {
95+
thing.singleKey = "hello";
96+
scope.inTransaction( em -> {
97+
em.persist( thing );
98+
} );
99+
fail();
100+
}
101+
catch (RollbackException re) {
102+
assertInstanceOf( ConstraintViolationException.class, re.getCause() );
103+
}
104+
}
105+
106+
@Test void testUniqueKey(EntityManagerFactoryScope scope) {
107+
scope.getEntityManagerFactory().getSchemaManager().truncate();
108+
This thisThing = new This();
109+
That thatThing = new That();
110+
thatThing.compositeKeyOne = 1;
111+
thatThing.compositeKeyTwo = 2;
112+
thatThing.singleKey = "hello";
113+
thatThing.theseOnSingleKey.add( thisThing );
114+
thatThing.theseOnCompositeKey.add( thisThing );
115+
scope.inTransaction( em -> {
116+
em.persist( thatThing );
117+
em.persist( thisThing );
118+
} );
119+
That thing = new That();
120+
thing.singleKey = "goodbye";
121+
thing.compositeKeyOne = 1;
122+
thing.compositeKeyTwo = 2;
123+
try {
124+
scope.inTransaction( em -> {
125+
em.persist( thing );
126+
} );
127+
fail();
128+
}
129+
catch (RollbackException re) {
130+
assertInstanceOf( ConstraintViolationException.class, re.getCause() );
131+
}
132+
}
133+
134+
@Entity
135+
static class That {
136+
@Id @GeneratedValue
137+
long id;
138+
139+
@Column(nullable = false)
140+
String singleKey;
141+
142+
int compositeKeyOne;
143+
int compositeKeyTwo;
144+
145+
@OneToMany
146+
@JoinColumn(referencedColumnName = "singleKey")
147+
Set<This> theseOnSingleKey = new HashSet<>();
148+
149+
@OneToMany
150+
@JoinColumn(referencedColumnName = "compositeKeyOne")
151+
@JoinColumn(referencedColumnName = "compositeKeyTwo")
152+
Set<This> theseOnCompositeKey = new HashSet<>();
153+
}
154+
155+
@Entity
156+
static class This {
157+
@Id @GeneratedValue
158+
long id;
159+
}
160+
}

hibernate-core/src/test/java/org/hibernate/orm/test/collection/NonPkCompositeJoinColumnCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public void testCollectionInsert(SessionFactoryScope scope) {
6565
Order order = new Order( "O1" );
6666
Item item = new Item( "Item 1" );
6767
order.addItem( item );
68-
session.persist( item );
6968
session.persist( order );
69+
session.persist( item );
7070
}
7171
);
7272
}

hibernate-core/src/test/java/org/hibernate/orm/test/collection/NonPkJoinColumnCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public void testInsertCollection(SessionFactoryScope scope) {
108108
Order order = new Order( "some_ref" );
109109
Item item = new Item( "Abc" );
110110
order.addItem( item );
111-
session.persist( item );
112111
session.persist( order );
112+
session.persist( item );
113113
session.flush();
114114
session.clear();
115115

hibernate-core/src/test/java/org/hibernate/orm/test/tool/schema/internal/StandardForeignKeyExporterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.orm.test.tool.schema.internal;
66

7-
import java.util.Collection;
87
import java.util.Optional;
98

109
import org.hibernate.boot.MetadataSources;

0 commit comments

Comments
 (0)