Skip to content

Commit 8cd9395

Browse files
gavinkingjrenaat
authored andcommitted
HHH-19555 add tests showing things actually working nicely
1 parent dfbd0bd commit 8cd9395

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.manytoone.jointable;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.JoinTable;
10+
import jakarta.persistence.ManyToOne;
11+
import org.hibernate.annotations.SQLRestriction;
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.JiraKey;
14+
import org.hibernate.testing.orm.junit.Jpa;
15+
import org.junit.jupiter.api.Test;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
20+
21+
@Jpa(annotatedClasses =
22+
{ManyToOneImplicitJoinTableRestrictionTest.X.class,
23+
ManyToOneImplicitJoinTableRestrictionTest.Y.class})
24+
class ManyToOneImplicitJoinTableRestrictionTest {
25+
@JiraKey("HHH-19555") @Test
26+
void test(EntityManagerFactoryScope scope) {
27+
scope.inTransaction( s -> {
28+
X x = new X();
29+
Y y = new Y();
30+
x.id = -1;
31+
y.x = x;
32+
s.persist( x );
33+
s.persist( y );
34+
} );
35+
scope.inTransaction( s -> {
36+
Y y = s.find( Y.class, 0L );
37+
y.name = "Gavin";
38+
assertNull(y.x);
39+
} );
40+
scope.inTransaction( s -> {
41+
Y y = s.find( Y.class, 0L );
42+
assertEquals("Gavin", y.name);
43+
assertNull(y.x);
44+
var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult();
45+
assertEquals( -1L, id );
46+
} );
47+
scope.inTransaction( s -> {
48+
Y y = s.find( Y.class, 0L );
49+
X x = new X();
50+
x.id = 1;
51+
s.persist( x );
52+
y.x = x;
53+
// uses a SQL merge to update the join table
54+
} );
55+
scope.inTransaction( s -> {
56+
Y y = s.find( Y.class, 0L );
57+
assertEquals("Gavin", y.name);
58+
assertNotNull(y.x);
59+
assertEquals( 1L, y.x.id );
60+
var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult();
61+
assertEquals( 1L, id );
62+
} );
63+
scope.inTransaction( s -> {
64+
Y y = s.find( Y.class, 0L );
65+
y.x = null;
66+
// uses a SQL merge to update the join table
67+
} );
68+
scope.inTransaction( s -> {
69+
Y y = s.find( Y.class, 0L );
70+
assertEquals("Gavin", y.name);
71+
assertNull(y.x);
72+
var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResultOrNull();
73+
assertNull( id );
74+
} );
75+
}
76+
77+
@Entity(name="Y")
78+
static class Y {
79+
@Id
80+
long id;
81+
String name;
82+
@JoinTable
83+
@ManyToOne X x;
84+
}
85+
@Entity(name="X")
86+
@SQLRestriction("id>0")
87+
static class X {
88+
@Id
89+
long id;
90+
}
91+
}

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytoone/jointable/ManyToOneImplicitJoinTableTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import org.junit.jupiter.api.Test;
1212

1313
import static org.junit.jupiter.api.Assertions.assertEquals;
14-
import static org.junit.jupiter.api.AssertionsKt.assertNotNull;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
import static org.junit.jupiter.api.Assertions.assertNull;
1516

1617
@Jpa(annotatedClasses =
1718
{ManyToOneImplicitJoinTableTest.X.class,
@@ -21,6 +22,7 @@ class ManyToOneImplicitJoinTableTest {
2122
void test(EntityManagerFactoryScope scope) {
2223
scope.inTransaction( s -> {
2324
X x = new X();
25+
x.id = 1;
2426
Y y = new Y();
2527
y.x = x;
2628
s.persist( x );
@@ -34,8 +36,34 @@ void test(EntityManagerFactoryScope scope) {
3436
Y y = s.find( Y.class, 0L );
3537
assertEquals("Gavin", y.name);
3638
assertNotNull(y.x);
39+
assertEquals( 1L, y.x.id );
40+
} );
41+
scope.inTransaction( s -> {
42+
Y y = s.find( Y.class, 0L );
43+
X x = new X();
44+
x.id = -1;
45+
s.persist( x );
46+
y.x = x;
47+
// uses a SQL merge to update the join table
48+
} );
49+
scope.inTransaction( s -> {
50+
Y y = s.find( Y.class, 0L );
51+
assertEquals("Gavin", y.name);
52+
assertNotNull(y.x);
53+
assertEquals( -1L, y.x.id );
54+
} );
55+
scope.inTransaction( s -> {
56+
Y y = s.find( Y.class, 0L );
57+
y.x = null;
58+
// uses a SQL merge to update the join table
59+
} );
60+
scope.inTransaction( s -> {
61+
Y y = s.find( Y.class, 0L );
62+
assertEquals("Gavin", y.name);
63+
assertNull(y.x);
3764
} );
3865
}
66+
3967
@Entity(name="Y")
4068
static class Y {
4169
@Id

0 commit comments

Comments
 (0)