|
| 1 | +package org.hibernate.orm.test.batch; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import org.hibernate.annotations.BatchSize; |
| 9 | +import org.hibernate.cfg.AvailableSettings; |
| 10 | + |
| 11 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.hibernate.testing.orm.junit.Setting; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import jakarta.persistence.CascadeType; |
| 22 | +import jakarta.persistence.Entity; |
| 23 | +import jakarta.persistence.FetchType; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.IdClass; |
| 26 | +import jakarta.persistence.JoinColumn; |
| 27 | +import jakarta.persistence.ManyToOne; |
| 28 | +import jakarta.persistence.OneToMany; |
| 29 | +import jakarta.persistence.Table; |
| 30 | +import org.assertj.core.api.Assertions; |
| 31 | + |
| 32 | +@ServiceRegistry( |
| 33 | + settings = { |
| 34 | + @Setting( name = AvailableSettings.DIALECT_NATIVE_PARAM_MARKERS, value = "false" ) |
| 35 | + } |
| 36 | +) |
| 37 | +@DomainModel( |
| 38 | + annotatedClasses = { |
| 39 | + BatchAndClassIdCollectionTest.Child.class, |
| 40 | + BatchAndClassIdCollectionTest.Parent.class |
| 41 | + } |
| 42 | +) |
| 43 | +@SessionFactory( |
| 44 | + useCollectingStatementInspector = true |
| 45 | +) |
| 46 | +@JiraKey("HHH-17202") |
| 47 | +public class BatchAndClassIdCollectionTest { |
| 48 | + |
| 49 | + @BeforeAll |
| 50 | + public void setUp(SessionFactoryScope scope) { |
| 51 | + scope.inTransaction( |
| 52 | + session -> { |
| 53 | + for (long i = 1L; i < 11; i++) { |
| 54 | + Parent parent = new Parent( i ); |
| 55 | + Child child = new Child( i * 100L + 1L, parent ); |
| 56 | + Child child2 = new Child( i * 100L + 2L, parent ); |
| 57 | + Child child3 = new Child( i * 100L + 3L, parent ); |
| 58 | + Child child4 = new Child( i * 100L + 4L, parent ); |
| 59 | + Child child5 = new Child( i * 100L + 5L, parent ); |
| 60 | + Child child6 = new Child( i * 100L + 6L, parent ); |
| 61 | + Child child7 = new Child( i * 100L + 7L, parent ); |
| 62 | + Child child8 = new Child( i * 100L + 8L, parent ); |
| 63 | + Child child9 = new Child( i * 100L + 9L, parent ); |
| 64 | + Child child10 = new Child( i * 100L + 10L, parent ); |
| 65 | + Child child11 = new Child( i * 100L + 11L, parent ); |
| 66 | + session.persist( parent ); |
| 67 | + } |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testBatchInitializeChildCollection(SessionFactoryScope scope){ |
| 74 | + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); |
| 75 | + scope.inTransaction( |
| 76 | + session -> { |
| 77 | + statementInspector.clear(); |
| 78 | + final List<Parent> list = session.createSelectionQuery( "from Parent", Parent.class ) |
| 79 | + .getResultList(); |
| 80 | + list.get( 0 ).getChildren().size(); |
| 81 | + statementInspector.assertExecutedCount( 2 ); |
| 82 | + Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).doesNotContain( "?" ); |
| 83 | + if ( scope.getSessionFactory().getJdbcServices().getDialect().useArrayForMultiValuedParameters() ) { |
| 84 | + Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "?" ); |
| 85 | + } |
| 86 | + else { |
| 87 | + Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "in (?,?,?,?,?)" ); |
| 88 | + } |
| 89 | + } |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + @Entity(name = "Child") |
| 94 | + @Table(name = "child_tablle") |
| 95 | + @IdClass(Child.IdClass.class) |
| 96 | + public static class Child { |
| 97 | + @Id |
| 98 | + private Long id; |
| 99 | + |
| 100 | + private String name; |
| 101 | + |
| 102 | + @ManyToOne |
| 103 | + @JoinColumn(name = "parent_id") |
| 104 | + private Parent parent; |
| 105 | + |
| 106 | + public Child() { |
| 107 | + } |
| 108 | + |
| 109 | + public Child(Long id, Parent parent) { |
| 110 | + this.id = id; |
| 111 | + this.name = String.valueOf( id ); |
| 112 | + this.parent = parent; |
| 113 | + parent.addChild( this ); |
| 114 | + } |
| 115 | + |
| 116 | + public Long getId() { |
| 117 | + return id; |
| 118 | + } |
| 119 | + |
| 120 | + public String getName() { |
| 121 | + return name; |
| 122 | + } |
| 123 | + |
| 124 | + public Parent getParent() { |
| 125 | + return parent; |
| 126 | + } |
| 127 | + |
| 128 | + public static class IdClass implements Serializable { |
| 129 | + private long id; |
| 130 | + |
| 131 | + public IdClass() { |
| 132 | + } |
| 133 | + |
| 134 | + public IdClass(long id) { |
| 135 | + this.id = id; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean equals(Object o) { |
| 140 | + if ( this == o ) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + if ( o == null || getClass() != o.getClass() ) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + Parent.IdClass idClass = (Parent.IdClass) o; |
| 148 | + |
| 149 | + return id == idClass.id; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public int hashCode() { |
| 154 | + return (int) ( id ^ ( id >>> 32 ) ); |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Entity(name = "Parent") |
| 161 | + @Table(name = "parents") |
| 162 | + @IdClass(Parent.IdClass.class) |
| 163 | + public static class Parent { |
| 164 | + @Id |
| 165 | + private Long id; |
| 166 | + |
| 167 | + private String name; |
| 168 | + |
| 169 | + @BatchSize(size = 5) |
| 170 | + @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) |
| 171 | + public Set<Child> children = new HashSet<>(); |
| 172 | + |
| 173 | + public Parent() { |
| 174 | + } |
| 175 | + |
| 176 | + public Parent(Long id) { |
| 177 | + this.id = id; |
| 178 | + this.name = String.valueOf( id ); |
| 179 | + } |
| 180 | + |
| 181 | + public Long getId() { |
| 182 | + return id; |
| 183 | + } |
| 184 | + |
| 185 | + public String getName() { |
| 186 | + return name; |
| 187 | + } |
| 188 | + |
| 189 | + public Set<Child> getChildren() { |
| 190 | + return children; |
| 191 | + } |
| 192 | + |
| 193 | + public void addChild(Child child){ |
| 194 | + children.add( child ); |
| 195 | + } |
| 196 | + |
| 197 | + public static class IdClass implements Serializable { |
| 198 | + private long id; |
| 199 | + |
| 200 | + public IdClass() { |
| 201 | + } |
| 202 | + |
| 203 | + public IdClass(long id) { |
| 204 | + this.id = id; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public boolean equals(Object o) { |
| 209 | + if ( this == o ) { |
| 210 | + return true; |
| 211 | + } |
| 212 | + if ( o == null || getClass() != o.getClass() ) { |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + IdClass idClass = (IdClass) o; |
| 217 | + |
| 218 | + return id == idClass.id; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public int hashCode() { |
| 223 | + return (int) ( id ^ ( id >>> 32 ) ); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments