Skip to content

Commit 6a97178

Browse files
committed
add more tests for @onDelete with various annotation-based mappings
1 parent cd55e5b commit 6a97178

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.ondeletecascade;
6+
7+
import jakarta.persistence.ElementCollection;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.Inheritance;
11+
import jakarta.persistence.InheritanceType;
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.annotations.OnDelete;
14+
import org.hibernate.annotations.OnDeleteAction;
15+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
16+
import org.hibernate.testing.orm.junit.Jpa;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
@Jpa(annotatedClasses =
27+
{OnDeleteCollectionTest.A.class},
28+
useCollectingStatementInspector = true)
29+
class OnDeleteCollectionTest {
30+
@Test void test(EntityManagerFactoryScope scope) {
31+
var inspector = scope.getCollectingStatementInspector();
32+
scope.inTransaction( em -> {
33+
A a = new A();
34+
a.id = 2;
35+
a.bs.add( "b" );
36+
em.persist( a );
37+
} );
38+
inspector.assertExecutedCount( 2 );
39+
inspector.clear();
40+
41+
scope.inTransaction( em -> {
42+
A a = em.find( A.class, 2L );
43+
inspector.assertExecutedCount( 1 );
44+
assertEquals( 1, a.bs.size() );
45+
inspector.assertExecutedCount( 2 );
46+
assertTrue( Hibernate.isInitialized( a.bs ) );
47+
} );
48+
inspector.clear();
49+
50+
scope.inTransaction( em -> {
51+
A a = em.find( A.class, 2L );
52+
inspector.assertExecutedCount( 1 );
53+
em.remove( a );
54+
assertFalse( Hibernate.isInitialized( a.bs ) );
55+
} );
56+
inspector.assertExecutedCount( 2 );
57+
58+
scope.inTransaction( em -> {
59+
assertEquals( 0,
60+
em.createNativeQuery( "select count(*) from A_bs", Integer.class )
61+
.getSingleResultOrNull() );
62+
assertEquals( 0,
63+
em.createNativeQuery( "select count(*) from A", Integer.class )
64+
.getSingleResultOrNull() );
65+
});
66+
}
67+
68+
@Entity(name = "A")
69+
@Inheritance(strategy = InheritanceType.JOINED)
70+
static class A {
71+
@Id
72+
long id;
73+
boolean a;
74+
@ElementCollection
75+
@OnDelete(action = OnDeleteAction.CASCADE)
76+
Set<String> bs = new HashSet<>();
77+
}
78+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.ondeletecascade;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Inheritance;
10+
import jakarta.persistence.InheritanceType;
11+
import org.hibernate.annotations.OnDelete;
12+
import org.hibernate.annotations.OnDeleteAction;
13+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
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+
19+
@Jpa(annotatedClasses =
20+
{OnDeleteJoinedInheritanceTest.A.class,
21+
OnDeleteJoinedInheritanceTest.B.class,
22+
OnDeleteJoinedInheritanceTest.C.class},
23+
useCollectingStatementInspector = true)
24+
class OnDeleteJoinedInheritanceTest {
25+
@Test void test(EntityManagerFactoryScope scope) {
26+
var inspector = scope.getCollectingStatementInspector();
27+
scope.inTransaction( em -> {
28+
B b = new B();
29+
b.id = 1;
30+
em.persist( b );
31+
C c = new C();
32+
c.id = 2;
33+
em.persist( c );
34+
} );
35+
inspector.assertExecutedCount( 4 );
36+
inspector.clear();
37+
38+
scope.inTransaction( em -> {
39+
A b = em.find( A.class, 1L );
40+
A c = em.getReference( A.class, 2L );
41+
inspector.assertExecutedCount( 1 );
42+
em.remove( b );
43+
em.remove( c );
44+
} );
45+
inspector.assertExecutedCount( 4 );
46+
47+
scope.inTransaction( em -> {
48+
assertEquals( 0,
49+
em.createNativeQuery( "select count(*) from B", Integer.class )
50+
.getSingleResultOrNull() );
51+
assertEquals( 0,
52+
em.createNativeQuery( "select count(*) from C", Integer.class )
53+
.getSingleResultOrNull() );
54+
assertEquals( 0,
55+
em.createNativeQuery( "select count(*) from A", Integer.class )
56+
.getSingleResultOrNull() );
57+
});
58+
}
59+
60+
@Entity(name = "A")
61+
@Inheritance(strategy = InheritanceType.JOINED)
62+
static class A {
63+
@Id
64+
long id;
65+
boolean a;
66+
}
67+
68+
@Entity(name = "B")
69+
@OnDelete(action = OnDeleteAction.CASCADE)
70+
static class B extends A {
71+
long b;
72+
}
73+
74+
@Entity(name = "C")
75+
@OnDelete(action = OnDeleteAction.CASCADE)
76+
static class C extends A {
77+
int c;
78+
}
79+
}
Lines changed: 91 additions & 0 deletions
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.ondeletecascade;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Inheritance;
10+
import jakarta.persistence.InheritanceType;
11+
import jakarta.persistence.ManyToMany;
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.annotations.OnDelete;
14+
import org.hibernate.annotations.OnDeleteAction;
15+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
16+
import org.hibernate.testing.orm.junit.Jpa;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
@Jpa(annotatedClasses =
27+
{OnDeleteManyToManyTest.A.class,
28+
OnDeleteManyToManyTest.B.class},
29+
useCollectingStatementInspector = true)
30+
class OnDeleteManyToManyTest {
31+
@Test void test(EntityManagerFactoryScope scope) {
32+
var inspector = scope.getCollectingStatementInspector();
33+
scope.inTransaction( em -> {
34+
B b = new B();
35+
b.id = 1;
36+
em.persist( b );
37+
A a = new A();
38+
a.id = 2;
39+
a.bs.add( b );
40+
em.persist( a );
41+
} );
42+
inspector.assertExecutedCount( 3 );
43+
inspector.clear();
44+
45+
scope.inTransaction( em -> {
46+
A a = em.find( A.class, 2L );
47+
inspector.assertExecutedCount( 1 );
48+
assertEquals( 1, a.bs.size() );
49+
inspector.assertExecutedCount( 2 );
50+
assertTrue( Hibernate.isInitialized( a.bs ) );
51+
} );
52+
inspector.clear();
53+
54+
scope.inTransaction( em -> {
55+
A a = em.find( A.class, 2L );
56+
inspector.assertExecutedCount( 1 );
57+
em.remove( a );
58+
assertFalse( Hibernate.isInitialized( a.bs ) );
59+
} );
60+
inspector.assertExecutedCount( 2 );
61+
62+
scope.inTransaction( em -> {
63+
assertEquals( 1,
64+
em.createNativeQuery( "select count(*) from B", Integer.class )
65+
.getSingleResultOrNull() );
66+
assertEquals( 0,
67+
em.createNativeQuery( "select count(*) from A_B", Integer.class )
68+
.getSingleResultOrNull() );
69+
assertEquals( 0,
70+
em.createNativeQuery( "select count(*) from A", Integer.class )
71+
.getSingleResultOrNull() );
72+
});
73+
}
74+
75+
@Entity(name = "A")
76+
@Inheritance(strategy = InheritanceType.JOINED)
77+
static class A {
78+
@Id
79+
long id;
80+
boolean a;
81+
@ManyToMany
82+
@OnDelete(action = OnDeleteAction.CASCADE)
83+
Set<B> bs = new HashSet<>();
84+
}
85+
86+
@Entity(name = "B")
87+
static class B {
88+
@Id
89+
long id;
90+
}
91+
}

0 commit comments

Comments
 (0)