Skip to content

Commit 550149c

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

File tree

3 files changed

+257
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)