Skip to content

Commit 0d0d849

Browse files
committed
run the @onDelete(CASCADE) tests also on Sybase
change the assertions about how many statements are executed in the cases covered in these tests, the external behavior is portable, which is great. On the other hand, there are also cases where @onDelete(CASCADE) is unportable, and perhaps in those cases we should detect and throw when @onDelete(CASCADE) is used on a platform with no 'on delete cascade' construct (i.e. on Sybase)
1 parent 7261f94 commit 0d0d849

File tree

6 files changed

+29
-28
lines changed

6 files changed

+29
-28
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteCascadeRemoveTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
import org.hibernate.SessionFactory;
1515
import org.hibernate.annotations.OnDelete;
1616
import org.hibernate.annotations.OnDeleteAction;
17-
import org.hibernate.annotations.SQLDelete;
1817
import org.hibernate.engine.spi.SessionImplementor;
1918
import org.hibernate.stat.EntityStatistics;
2019
import org.hibernate.stat.Statistics;
21-
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
2220
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
2321
import org.hibernate.testing.orm.junit.Jpa;
24-
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
2522
import org.junit.jupiter.api.Test;
2623

2724
import java.util.HashSet;
@@ -36,15 +33,16 @@
3633
OnDeleteCascadeRemoveTest.Child.class},
3734
generateStatistics = true,
3835
useCollectingStatementInspector = true)
39-
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
36+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
4037
class OnDeleteCascadeRemoveTest {
4138
@Test
4239
void testOnDeleteCascadeRemove1(EntityManagerFactoryScope scope) {
4340
Statistics statistics =
4441
scope.getEntityManagerFactory().unwrap( SessionFactory.class )
4542
.getStatistics();
4643
statistics.clear();
47-
scope.getCollectingStatementInspector().clear();
44+
var inspector = scope.getCollectingStatementInspector();
45+
inspector.clear();
4846
scope.inTransaction( em -> {
4947
Parent parent = new Parent();
5048
Child child = new Child();
@@ -62,7 +60,7 @@ void testOnDeleteCascadeRemove1(EntityManagerFactoryScope scope) {
6260
EntityStatistics entityStatistics = statistics.getEntityStatistics( Child.class.getName() );
6361
assertEquals( 1L, entityStatistics.getDeleteCount() );
6462
assertEquals( 1L, entityStatistics.getCacheRemoveCount() );
65-
assertEquals( 5, scope.getCollectingStatementInspector().getSqlQueries().size() );
63+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 5 : 6 );
6664
long children =
6765
scope.fromTransaction( em -> em.createQuery( "select count(*) from CascadeChild", Long.class )
6866
.getSingleResult() );
@@ -75,7 +73,8 @@ void testOnDeleteCascadeRemove2(EntityManagerFactoryScope scope) {
7573
scope.getEntityManagerFactory().unwrap( SessionFactory.class )
7674
.getStatistics();
7775
statistics.clear();
78-
scope.getCollectingStatementInspector().clear();
76+
var inspector = scope.getCollectingStatementInspector();
77+
inspector.clear();
7978
scope.inTransaction( em -> {
8079
Parent parent = new Parent();
8180
Child child = new Child();
@@ -96,7 +95,7 @@ void testOnDeleteCascadeRemove2(EntityManagerFactoryScope scope) {
9695
EntityStatistics entityStatistics = statistics.getEntityStatistics( Child.class.getName() );
9796
assertEquals( 1L, entityStatistics.getDeleteCount() );
9897
assertEquals( 1L, entityStatistics.getCacheRemoveCount() );
99-
assertEquals( 5, scope.getCollectingStatementInspector().getSqlQueries().size() );
98+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 5 : 6 );
10099
long children =
101100
scope.fromTransaction( em -> em.createQuery( "select count(*) from CascadeChild", Long.class )
102101
.getSingleResult() );
@@ -114,7 +113,7 @@ static class Parent {
114113
}
115114
@Entity(name="CascadeChild")
116115
@Cacheable
117-
@SQLDelete( sql = "should never happen" )
116+
// @SQLDelete( sql = "should never happen" )
118117
static class Child {
119118
@Id
120119
long id;

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteCollectionTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import org.hibernate.Hibernate;
1313
import org.hibernate.annotations.OnDelete;
1414
import org.hibernate.annotations.OnDeleteAction;
15-
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1615
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1716
import org.hibernate.testing.orm.junit.Jpa;
18-
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1917
import org.junit.jupiter.api.Test;
2018

2119
import java.util.HashSet;
@@ -28,7 +26,7 @@
2826
@Jpa(annotatedClasses =
2927
{OnDeleteCollectionTest.A.class},
3028
useCollectingStatementInspector = true)
31-
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
29+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
3230
class OnDeleteCollectionTest {
3331
@Test void test(EntityManagerFactoryScope scope) {
3432
var inspector = scope.getCollectingStatementInspector();
@@ -56,7 +54,7 @@ class OnDeleteCollectionTest {
5654
em.remove( a );
5755
assertFalse( Hibernate.isInitialized( a.bs ) );
5856
} );
59-
inspector.assertExecutedCount( 2 );
57+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 2 : 3 );
6058

6159
scope.inTransaction( em -> {
6260
assertEquals( 0,

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteJoinedInheritanceTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
import jakarta.persistence.InheritanceType;
1111
import org.hibernate.annotations.OnDelete;
1212
import org.hibernate.annotations.OnDeleteAction;
13-
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1413
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1514
import org.hibernate.testing.orm.junit.Jpa;
16-
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1715
import org.junit.jupiter.api.Test;
1816

1917
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -23,7 +21,7 @@
2321
OnDeleteJoinedInheritanceTest.B.class,
2422
OnDeleteJoinedInheritanceTest.C.class},
2523
useCollectingStatementInspector = true)
26-
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
24+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
2725
class OnDeleteJoinedInheritanceTest {
2826
@Test void test(EntityManagerFactoryScope scope) {
2927
var inspector = scope.getCollectingStatementInspector();
@@ -45,7 +43,7 @@ class OnDeleteJoinedInheritanceTest {
4543
em.remove( b );
4644
em.remove( c );
4745
} );
48-
inspector.assertExecutedCount( 4 );
46+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 4 : 6 );
4947

5048
scope.inTransaction( em -> {
5149
assertEquals( 0,

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToManyTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import org.hibernate.Hibernate;
1313
import org.hibernate.annotations.OnDelete;
1414
import org.hibernate.annotations.OnDeleteAction;
15-
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1615
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1716
import org.hibernate.testing.orm.junit.Jpa;
18-
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1917
import org.junit.jupiter.api.Test;
2018

2119
import java.util.HashSet;
@@ -29,7 +27,7 @@
2927
{OnDeleteManyToManyTest.A.class,
3028
OnDeleteManyToManyTest.B.class},
3129
useCollectingStatementInspector = true)
32-
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
30+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
3331
class OnDeleteManyToManyTest {
3432
@Test void test(EntityManagerFactoryScope scope) {
3533
var inspector = scope.getCollectingStatementInspector();
@@ -60,7 +58,7 @@ class OnDeleteManyToManyTest {
6058
em.remove( a );
6159
assertFalse( Hibernate.isInitialized( a.bs ) );
6260
} );
63-
inspector.assertExecutedCount( 2 );
61+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 2 : 3 );
6462

6563
scope.inTransaction( em -> {
6664
assertEquals( 1,

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteTest.java renamed to hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToOneTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import static org.hibernate.annotations.OnDeleteAction.CASCADE;
2424
import static org.junit.jupiter.api.Assertions.assertNull;
2525

26-
@Jpa(annotatedClasses = {OnDeleteTest.Parent.class, OnDeleteTest.Child.class})
26+
@Jpa(annotatedClasses = {OnDeleteManyToOneTest.Parent.class, OnDeleteManyToOneTest.Child.class})
2727
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
28-
public class OnDeleteTest {
28+
public class OnDeleteManyToOneTest {
2929
@Test
3030
public void testOnDelete(EntityManagerFactoryScope scope) {
3131
Parent parent = new Parent();

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteTest2.java renamed to hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteOneToManyTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
import jakarta.persistence.JoinColumn;
1010
import jakarta.persistence.OneToMany;
1111
import jakarta.persistence.RollbackException;
12+
import org.hibernate.Hibernate;
1213
import org.hibernate.TransientObjectException;
1314
import org.hibernate.annotations.OnDelete;
1415
import org.hibernate.annotations.OnDeleteAction;
15-
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1616
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1717
import org.hibernate.testing.orm.junit.Jpa;
18-
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1918
import org.junit.jupiter.api.AfterEach;
2019
import org.junit.jupiter.api.Test;
2120

@@ -27,22 +26,31 @@
2726
import static org.junit.jupiter.api.Assertions.assertTrue;
2827
import static org.junit.jupiter.api.Assertions.fail;
2928

30-
@Jpa(annotatedClasses = {OnDeleteTest2.Parent.class, OnDeleteTest2.Child.class})
31-
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
32-
public class OnDeleteTest2 {
29+
@Jpa(annotatedClasses =
30+
{OnDeleteOneToManyTest.Parent.class, OnDeleteOneToManyTest.Child.class},
31+
useCollectingStatementInspector = true)
32+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
33+
public class OnDeleteOneToManyTest {
3334
@Test
3435
public void testOnDeleteParent(EntityManagerFactoryScope scope) {
36+
var inspector = scope.getCollectingStatementInspector();
37+
inspector.clear();
3538
Parent parent = new Parent();
3639
Child child = new Child();
3740
parent.children.add( child );
3841
scope.inTransaction( em -> {
3942
em.persist( parent );
4043
em.persist( child );
4144
} );
45+
inspector.assertExecutedCount( 3 );
46+
inspector.clear();
4247
scope.inTransaction( em -> {
4348
Parent p = em.find( Parent.class, parent.id );
49+
inspector.assertExecutedCount( 1 );
50+
assertTrue( Hibernate.isInitialized( p.children ) );
4451
em.remove( p );
4552
} );
53+
inspector.assertExecutedCount( 3 );
4654
scope.inTransaction( em -> {
4755
// since it's an owned collection, the FK gets set to null
4856
assertNotNull( em.find( Child.class, child.id ) );

0 commit comments

Comments
 (0)