|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.annotations.collectionelement; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Column; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.FetchType; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.JoinColumn; |
| 13 | +import jakarta.persistence.JoinColumns; |
| 14 | +import jakarta.persistence.ManyToOne; |
| 15 | +import jakarta.persistence.OneToMany; |
| 16 | +import org.hibernate.cfg.BatchSettings; |
| 17 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 20 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 23 | +import org.hibernate.testing.orm.junit.Setting; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 32 | + |
| 33 | + |
| 34 | +@DomainModel( |
| 35 | + annotatedClasses = { |
| 36 | + TestCollectionWithOrderInsertAndOrderUpdates.ChildEntity.class, |
| 37 | + TestCollectionWithOrderInsertAndOrderUpdates.ParentEntity.class, |
| 38 | + TestCollectionWithOrderInsertAndOrderUpdates.AnotherEntity.class, |
| 39 | + TestCollectionWithOrderInsertAndOrderUpdates.ThirdEntity.class, |
| 40 | + } |
| 41 | +) |
| 42 | +@SessionFactory |
| 43 | +@ServiceRegistry( |
| 44 | + settings = { |
| 45 | + @Setting(name = BatchSettings.ORDER_INSERTS, value = "true"), |
| 46 | + @Setting(name = BatchSettings.ORDER_UPDATES, value = "true"), |
| 47 | + } |
| 48 | +) |
| 49 | +@BytecodeEnhanced(runNotEnhancedAsWell = true) |
| 50 | +@JiraKey("HHH-19585") |
| 51 | +public class TestCollectionWithOrderInsertAndOrderUpdates { |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + public void setUp(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( |
| 56 | + session -> { |
| 57 | + ParentEntity parentEntity = new ParentEntity( 1L, "Parent 1", 0L ); |
| 58 | + parentEntity.addChild( new ChildEntity( 1L, "Child 1" ) ); |
| 59 | + session.persist( parentEntity ); |
| 60 | + |
| 61 | + ParentEntity parentEntity2 = new ParentEntity( 2l, "Parent 2", 0L ); |
| 62 | + parentEntity2.addChild( new ChildEntity( 2L, "Child 2" ) ); |
| 63 | + session.persist( parentEntity2 ); |
| 64 | + |
| 65 | + ParentEntity parentEntity3 = new ParentEntity( 3l, "Parent 3", 0L ); |
| 66 | + session.persist( parentEntity3 ); |
| 67 | + |
| 68 | + AnotherEntity anotherEntity = new AnotherEntity( 1L, "1", 1L, parentEntity ); |
| 69 | + |
| 70 | + AnotherEntity anotherEntity2 = new AnotherEntity( 2L, "2", 2L, parentEntity2 ); |
| 71 | + AnotherEntity anotherEntity3 = new AnotherEntity( 3L, "3", 3L, parentEntity3 ); |
| 72 | + |
| 73 | + ThirdEntity thirdEntity = new ThirdEntity( 1L, "123" ); |
| 74 | + thirdEntity.addAnotherEntity( anotherEntity ); |
| 75 | + thirdEntity.addAnotherEntity( anotherEntity2 ); |
| 76 | + thirdEntity.addAnotherEntity( anotherEntity3 ); |
| 77 | + |
| 78 | + session.persist( thirdEntity ); |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetReference(SessionFactoryScope scope) { |
| 85 | + scope.inTransaction( |
| 86 | + session -> { |
| 87 | + ThirdEntity thirdEntity = session.getReference( ThirdEntity.class, 1L ); |
| 88 | + for ( AnotherEntity thirdEntityDetail : thirdEntity.getAnotherEntities() ) { |
| 89 | + thirdEntityDetail.getParentEntity().getName(); |
| 90 | + } |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + scope.inTransaction( |
| 95 | + session -> { |
| 96 | + ParentEntity parentEntity = session.find( ParentEntity.class, 1L ); |
| 97 | + assertThat( parentEntity.getChildren().size() ).isEqualTo( 1 ); |
| 98 | + |
| 99 | + ParentEntity parentEntity2 = session.find( ParentEntity.class, 2L ); |
| 100 | + assertThat( parentEntity2.getChildren().size() ).isEqualTo( 1 ); |
| 101 | + |
| 102 | + ParentEntity parentEntity3 = session.find( ParentEntity.class, 3L ); |
| 103 | + assertThat( parentEntity3.getChildren().size() ).isEqualTo( 0 ); |
| 104 | + } |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @AfterEach |
| 109 | + public void tearDown(SessionFactoryScope scope) { |
| 110 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 111 | + } |
| 112 | + |
| 113 | + @Entity(name = "ChildEntity") |
| 114 | + public static class ChildEntity { |
| 115 | + @Id |
| 116 | + private Long id; |
| 117 | + |
| 118 | + private String name; |
| 119 | + |
| 120 | + public ChildEntity() { |
| 121 | + } |
| 122 | + |
| 123 | + public ChildEntity(Long id, String name) { |
| 124 | + this.id = id; |
| 125 | + this.name = name; |
| 126 | + } |
| 127 | + |
| 128 | + public Long getId() { |
| 129 | + return id; |
| 130 | + } |
| 131 | + |
| 132 | + public void setId(Long id) { |
| 133 | + this.id = id; |
| 134 | + } |
| 135 | + |
| 136 | + public String getName() { |
| 137 | + return name; |
| 138 | + } |
| 139 | + |
| 140 | + public void setName(String name) { |
| 141 | + this.name = name; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Entity(name = "ParentEntity") |
| 146 | + public static class ParentEntity { |
| 147 | + @Id |
| 148 | + private Long id; |
| 149 | + |
| 150 | + private String name; |
| 151 | + |
| 152 | + @Column(name = "secondid") |
| 153 | + private Long secondId; |
| 154 | + |
| 155 | + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) |
| 156 | + @JoinColumns({ |
| 157 | + @JoinColumn(name = "parentid", referencedColumnName = "id"), |
| 158 | + @JoinColumn(name = "secondid", referencedColumnName = "secondid") |
| 159 | + }) |
| 160 | + private List<ChildEntity> children; |
| 161 | + |
| 162 | + public ParentEntity() { |
| 163 | + } |
| 164 | + |
| 165 | + public ParentEntity(Long id, String name, Long secondId) { |
| 166 | + this.id = id; |
| 167 | + this.name = name; |
| 168 | + this.secondId = secondId; |
| 169 | + } |
| 170 | + |
| 171 | + public Long getId() { |
| 172 | + return id; |
| 173 | + } |
| 174 | + |
| 175 | + public String getName() { |
| 176 | + return name; |
| 177 | + } |
| 178 | + |
| 179 | + public List<ChildEntity> getChildren() { |
| 180 | + return children; |
| 181 | + } |
| 182 | + |
| 183 | + public void addChild(ChildEntity childEntity) { |
| 184 | + if ( this.children == null ) { |
| 185 | + this.children = new ArrayList<>(); |
| 186 | + } |
| 187 | + this.children.add( childEntity ); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Entity(name = "AnotherEntity") |
| 192 | + public static class AnotherEntity { |
| 193 | + @Id |
| 194 | + private Long id; |
| 195 | + |
| 196 | + private String name; |
| 197 | + |
| 198 | + @Column(name = "parentid") |
| 199 | + private Long parentId; |
| 200 | + |
| 201 | + @ManyToOne(fetch = FetchType.LAZY) |
| 202 | + @JoinColumn(name = "parentid", insertable = false, updatable = false, referencedColumnName = "id") |
| 203 | + private ParentEntity parentEntity; |
| 204 | + |
| 205 | + public AnotherEntity() { |
| 206 | + } |
| 207 | + |
| 208 | + public AnotherEntity(Long id, String name, Long parentId, ParentEntity parentEntity) { |
| 209 | + this.id = id; |
| 210 | + this.name = name; |
| 211 | + this.parentId = parentId; |
| 212 | + this.parentEntity = parentEntity; |
| 213 | + } |
| 214 | + |
| 215 | + public Long getId() { |
| 216 | + return id; |
| 217 | + } |
| 218 | + |
| 219 | + public String getName() { |
| 220 | + return name; |
| 221 | + } |
| 222 | + |
| 223 | + public Long getParentId() { |
| 224 | + return parentId; |
| 225 | + } |
| 226 | + |
| 227 | + public ParentEntity getParentEntity() { |
| 228 | + return parentEntity; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + @Entity(name = "ThirdEntity") |
| 233 | + public static class ThirdEntity { |
| 234 | + @Id |
| 235 | + private Long id; |
| 236 | + |
| 237 | + private String name; |
| 238 | + |
| 239 | + @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) |
| 240 | + @JoinColumn(name = "thirdEntityHeader", referencedColumnName = "id") |
| 241 | + private List<AnotherEntity> anotherEntities; |
| 242 | + |
| 243 | + public ThirdEntity() { |
| 244 | + } |
| 245 | + |
| 246 | + public ThirdEntity(Long id, String name) { |
| 247 | + this.id = id; |
| 248 | + this.name = name; |
| 249 | + } |
| 250 | + |
| 251 | + public Long getId() { |
| 252 | + return id; |
| 253 | + } |
| 254 | + |
| 255 | + public String getName() { |
| 256 | + return name; |
| 257 | + } |
| 258 | + |
| 259 | + public List<AnotherEntity> getAnotherEntities() { |
| 260 | + return anotherEntities; |
| 261 | + } |
| 262 | + |
| 263 | + public void addAnotherEntity(AnotherEntity anotherEntity) { |
| 264 | + if ( this.anotherEntities == null ) { |
| 265 | + this.anotherEntities = new ArrayList<>(); |
| 266 | + } |
| 267 | + this.anotherEntities.add( anotherEntity ); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + |
| 272 | +} |
0 commit comments