Skip to content

Commit b342616

Browse files
committed
HHH-18936 Fix sybase failing tests and add more tests
1 parent 8cb8ea8 commit b342616

File tree

5 files changed

+207
-21
lines changed

5 files changed

+207
-21
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
2-
* SPDX-License-Identifier: Apache-2.0
3-
* Copyright Red Hat Inc. and Hibernate Authors
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
46
*/
57
package org.hibernate.orm.test.ondeletecascade;
68

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.ondeletecascade;
8+
9+
import org.hibernate.Hibernate;
10+
import org.hibernate.annotations.OnDelete;
11+
import org.hibernate.annotations.OnDeleteAction;
12+
13+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
14+
import org.hibernate.testing.orm.junit.Jira;
15+
import org.hibernate.testing.orm.junit.Jpa;
16+
import org.junit.jupiter.api.Test;
17+
18+
import jakarta.persistence.ElementCollection;
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.Id;
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+
class OnDeleteCollectionTest {
32+
@Test
33+
void test(EntityManagerFactoryScope scope) {
34+
var inspector = scope.getCollectingStatementInspector();
35+
inspector.clear();
36+
37+
scope.inTransaction( em -> {
38+
A a = new A();
39+
a.id = 2;
40+
a.bs.add( "b" );
41+
em.persist( a );
42+
} );
43+
inspector.assertExecutedCount( 2 );
44+
inspector.clear();
45+
46+
scope.inTransaction( em -> {
47+
A a = em.find( A.class, 2L );
48+
inspector.assertExecutedCount( 1 );
49+
assertEquals( 1, a.bs.size() );
50+
inspector.assertExecutedCount( 2 );
51+
assertTrue( Hibernate.isInitialized( a.bs ) );
52+
} );
53+
inspector.clear();
54+
55+
scope.inTransaction( em -> {
56+
A a = em.find( A.class, 2L );
57+
inspector.assertExecutedCount( 1 );
58+
em.remove( a );
59+
assertFalse( Hibernate.isInitialized( a.bs ) );
60+
} );
61+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 2 : 3 );
62+
63+
scope.inTransaction( em -> {
64+
assertEquals( 0,
65+
em.createNativeQuery( "select count(*) from A_bs", Integer.class )
66+
.getSingleResult() );
67+
assertEquals( 0,
68+
em.createNativeQuery( "select count(*) from A", Integer.class )
69+
.getSingleResult() );
70+
});
71+
}
72+
73+
@Entity(name = "A")
74+
static class A {
75+
@Id
76+
long id;
77+
boolean a;
78+
@ElementCollection
79+
@OnDelete(action = OnDeleteAction.CASCADE)
80+
Set<String> bs = new HashSet<>();
81+
}
82+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.ondeletecascade;
8+
9+
import org.hibernate.annotations.OnDelete;
10+
import org.hibernate.annotations.OnDeleteAction;
11+
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.Test;
15+
16+
import jakarta.persistence.Entity;
17+
import jakarta.persistence.Id;
18+
import jakarta.persistence.Inheritance;
19+
import jakarta.persistence.InheritanceType;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
@Jpa(annotatedClasses =
24+
{OnDeleteJoinedInheritanceTest.A.class,
25+
OnDeleteJoinedInheritanceTest.B.class,
26+
OnDeleteJoinedInheritanceTest.C.class},
27+
useCollectingStatementInspector = true)
28+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
29+
class OnDeleteJoinedInheritanceTest {
30+
@Test void test(EntityManagerFactoryScope scope) {
31+
var inspector = scope.getCollectingStatementInspector();
32+
scope.inTransaction( em -> {
33+
B b = new B();
34+
b.id = 1;
35+
em.persist( b );
36+
C c = new C();
37+
c.id = 2;
38+
em.persist( c );
39+
} );
40+
inspector.assertExecutedCount( 4 );
41+
inspector.clear();
42+
43+
scope.inTransaction( em -> {
44+
A b = em.find( A.class, 1L );
45+
A c = em.getReference( A.class, 2L );
46+
inspector.assertExecutedCount( 1 );
47+
em.remove( b );
48+
em.remove( c );
49+
} );
50+
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 4 : 6 );
51+
52+
scope.inTransaction( em -> {
53+
assertEquals( 0,
54+
em.createNativeQuery( "select count(*) from B", Integer.class )
55+
.getSingleResult() );
56+
assertEquals( 0,
57+
em.createNativeQuery( "select count(*) from C", Integer.class )
58+
.getSingleResult() );
59+
assertEquals( 0,
60+
em.createNativeQuery( "select count(*) from A", Integer.class )
61+
.getSingleResult() );
62+
});
63+
}
64+
65+
@Entity(name = "A")
66+
@Inheritance(strategy = InheritanceType.JOINED)
67+
static class A {
68+
@Id
69+
long id;
70+
boolean a;
71+
}
72+
73+
@Entity(name = "B")
74+
@OnDelete(action = OnDeleteAction.CASCADE)
75+
static class B extends A {
76+
long b;
77+
}
78+
79+
@Entity(name = "C")
80+
@OnDelete(action = OnDeleteAction.CASCADE)
81+
static class C extends A {
82+
int c;
83+
}
84+
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
/*
2-
* SPDX-License-Identifier: Apache-2.0
3-
* Copyright Red Hat Inc. and Hibernate Authors
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
46
*/
5-
package org.hibernate.orm.test;
7+
package org.hibernate.orm.test.ondeletecascade;
68

7-
import jakarta.persistence.Entity;
8-
import jakarta.persistence.Id;
9-
import jakarta.persistence.ManyToOne;
10-
import jakarta.persistence.OneToMany;
119
import org.hibernate.annotations.OnDelete;
1210
import org.hibernate.internal.SessionFactoryImpl;
1311

12+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1413
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1514
import org.hibernate.testing.orm.junit.Jpa;
15+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1616
import org.junit.jupiter.api.AfterEach;
1717
import org.junit.jupiter.api.Test;
1818

19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.ManyToOne;
22+
import jakarta.persistence.OneToMany;
1923
import java.util.HashSet;
2024
import java.util.Set;
2125

2226
import static jakarta.persistence.FetchType.EAGER;
2327
import static org.hibernate.annotations.OnDeleteAction.CASCADE;
2428
import static org.junit.jupiter.api.Assertions.assertNull;
2529

26-
@Jpa(annotatedClasses = {OnDeleteTest.Parent.class, OnDeleteTest.Child.class})
27-
public class OnDeleteTest {
30+
@Jpa(annotatedClasses = {OnDeleteManyToOneTest.Parent.class, OnDeleteManyToOneTest.Child.class})
31+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
32+
public class OnDeleteManyToOneTest {
2833
@Test
2934
public void testOnDelete(EntityManagerFactoryScope scope) {
3035
Parent parent = new Parent();

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/*
2-
* SPDX-License-Identifier: Apache-2.0
3-
* Copyright Red Hat Inc. and Hibernate Authors
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
46
*/
5-
package org.hibernate.orm.test;
7+
package org.hibernate.orm.test.ondeletecascade;
68

7-
import jakarta.persistence.Entity;
8-
import jakarta.persistence.Id;
9-
import jakarta.persistence.JoinColumn;
10-
import jakarta.persistence.OneToMany;
11-
import jakarta.persistence.RollbackException;
9+
import org.hibernate.Hibernate;
1210
import org.hibernate.TransientObjectException;
1311
import org.hibernate.annotations.OnDelete;
1412
import org.hibernate.annotations.OnDeleteAction;
@@ -19,6 +17,11 @@
1917
import org.junit.jupiter.api.AfterEach;
2018
import org.junit.jupiter.api.Test;
2119

20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.Id;
22+
import jakarta.persistence.JoinColumn;
23+
import jakarta.persistence.OneToMany;
24+
import jakarta.persistence.RollbackException;
2225
import java.util.HashSet;
2326
import java.util.Set;
2427

@@ -27,21 +30,31 @@
2730
import static org.junit.jupiter.api.Assertions.assertTrue;
2831
import static org.junit.jupiter.api.Assertions.fail;
2932

30-
@Jpa(annotatedClasses = {OnDeleteTest2.Parent.class, OnDeleteTest2.Child.class})
31-
public class OnDeleteTest2 {
33+
@Jpa(annotatedClasses =
34+
{OnDeleteOneToManyTest.Parent.class, OnDeleteOneToManyTest.Child.class},
35+
useCollectingStatementInspector = true)
36+
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
37+
public class OnDeleteOneToManyTest {
3238
@Test
3339
public void testOnDeleteParent(EntityManagerFactoryScope scope) {
40+
var inspector = scope.getCollectingStatementInspector();
41+
inspector.clear();
3442
Parent parent = new Parent();
3543
Child child = new Child();
3644
parent.children.add( child );
3745
scope.inTransaction( em -> {
3846
em.persist( parent );
3947
em.persist( child );
4048
} );
49+
inspector.assertExecutedCount( 3 );
50+
inspector.clear();
4151
scope.inTransaction( em -> {
4252
Parent p = em.find( Parent.class, parent.id );
53+
inspector.assertExecutedCount( 1 );
54+
assertTrue( Hibernate.isInitialized( p.children ) );
4355
em.remove( p );
4456
} );
57+
inspector.assertExecutedCount( 3 );
4558
scope.inTransaction( em -> {
4659
// since it's an owned collection, the FK gets set to null
4760
assertNotNull( em.find( Child.class, child.id ) );

0 commit comments

Comments
 (0)