|
| 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