Skip to content

Commit c2ee076

Browse files
committed
Change the way persisting works as needed for list semantics and throw
proper error on null list index
1 parent 62b4d24 commit c2ee076

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/ArrayInitializer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.engine.spi.CollectionKey;
1414
import org.hibernate.internal.log.LoggingHelper;
1515
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
16+
import org.hibernate.query.SemanticException;
1617
import org.hibernate.query.spi.NavigablePath;
1718
import org.hibernate.sql.results.graph.DomainResultAssembler;
1819
import org.hibernate.sql.results.graph.FetchParentAccess;
@@ -66,7 +67,11 @@ protected void readCollectionRow(
6667
CollectionKey collectionKey,
6768
List<Object> loadingState,
6869
RowProcessingState rowProcessingState) {
69-
int index = listIndexAssembler.assemble( rowProcessingState );
70+
final Integer indexValue = listIndexAssembler.assemble( rowProcessingState );
71+
if ( indexValue == null ) {
72+
throw new SemanticException( "Illegal null index value encountered while reading: " + getCollectionAttributeMapping().getNavigableRole() );
73+
}
74+
int index = indexValue;
7075

7176
if ( indexBase != 0 ) {
7277
index -= indexBase;

hibernate-core/src/main/java/org/hibernate/sql/results/graph/collection/internal/ListInitializer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.engine.spi.CollectionKey;
1414
import org.hibernate.internal.log.LoggingHelper;
1515
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
16+
import org.hibernate.query.SemanticException;
1617
import org.hibernate.query.spi.NavigablePath;
1718
import org.hibernate.sql.results.graph.DomainResultAssembler;
1819
import org.hibernate.sql.results.graph.FetchParentAccess;
@@ -68,7 +69,11 @@ protected void readCollectionRow(
6869
CollectionKey collectionKey,
6970
List<Object> loadingState,
7071
RowProcessingState rowProcessingState) {
71-
int index = listIndexAssembler.assemble( rowProcessingState );
72+
final Integer indexValue = listIndexAssembler.assemble( rowProcessingState );
73+
if ( indexValue == null ) {
74+
throw new SemanticException( "Illegal null index value encountered while reading: " + getCollectionAttributeMapping().getNavigableRole() );
75+
}
76+
int index = indexValue;
7277

7378
if ( listIndexBase != 0 ) {
7479
index -= listIndexBase;

hibernate-core/src/test/java/org/hibernate/orm/test/ops/OneToManyMappedByCascadeDeleteTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public void testRemoveCascadeDelete(SessionFactoryScope scope) {
5555
child.setId( 2 );
5656
child.setParent( parent );
5757
s.persist( child );
58+
59+
parent.children.add( child );
5860
} );
5961

6062
getStatistics( scope ).clear();
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.ops;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import org.hibernate.annotations.Cascade;
13+
import org.hibernate.annotations.CascadeType;
14+
import org.hibernate.query.SemanticException;
15+
import org.hibernate.stat.spi.StatisticsImplementor;
16+
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
24+
import jakarta.persistence.Entity;
25+
import jakarta.persistence.FetchType;
26+
import jakarta.persistence.Id;
27+
import jakarta.persistence.ManyToOne;
28+
import jakarta.persistence.OneToMany;
29+
import jakarta.persistence.OrderColumn;
30+
31+
import static org.hamcrest.CoreMatchers.is;
32+
import static org.hamcrest.MatcherAssert.assertThat;
33+
34+
35+
@SessionFactory
36+
@DomainModel(annotatedClasses = {
37+
OneToManyMappedByOrderColumnTest.Parent.class,
38+
OneToManyMappedByOrderColumnTest.Child.class
39+
})
40+
public class OneToManyMappedByOrderColumnTest {
41+
42+
@AfterEach
43+
public void cleanup(SessionFactoryScope scope) {
44+
scope.inTransaction( s -> {
45+
s.createMutationQuery( "delete from child" ).executeUpdate();
46+
s.createMutationQuery( "delete from parent" ).executeUpdate();
47+
} );
48+
}
49+
50+
@Test
51+
public void testReadNullIndex(SessionFactoryScope scope) {
52+
scope.inTransaction( s -> {
53+
Parent parent = new Parent();
54+
parent.setId( 1 );
55+
s.persist( parent );
56+
57+
Child child = new Child();
58+
child.setId( 2 );
59+
child.setParent( parent );
60+
s.persist( child );
61+
} );
62+
63+
scope.inTransaction( s -> {
64+
try {
65+
s.get( Parent.class, 1 );
66+
Assertions.fail( "Expected to fail because list index is null" );
67+
}
68+
catch (SemanticException ex) {
69+
Assertions.assertTrue( ex.getMessage().contains( "children" ) );
70+
}
71+
} );
72+
}
73+
74+
@Entity(name = "parent")
75+
public static class Parent {
76+
77+
@Id
78+
private Integer id;
79+
@OrderColumn(name = "list_idx")
80+
@OneToMany(targetEntity = Child.class, mappedBy = "parent", fetch = FetchType.EAGER)
81+
@Cascade(CascadeType.DELETE)
82+
private List<Child> children = new ArrayList<>();
83+
84+
public Integer getId() {
85+
return id;
86+
}
87+
88+
public void setId(Integer id) {
89+
this.id = id;
90+
}
91+
92+
public List<Child> getChildren() {
93+
return children;
94+
}
95+
96+
public void setChildren(List<Child> children) {
97+
this.children = children;
98+
}
99+
}
100+
101+
@Entity(name = "child")
102+
public static class Child {
103+
104+
@Id
105+
private Integer id;
106+
@ManyToOne(targetEntity = Parent.class, fetch = FetchType.LAZY)
107+
private Parent parent;
108+
109+
public Integer getId() {
110+
return id;
111+
}
112+
113+
public void setId(Integer id) {
114+
this.id = id;
115+
}
116+
117+
public Parent getParent() {
118+
return parent;
119+
}
120+
121+
public void setParent(Parent parent) {
122+
this.parent = parent;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)