|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.UUID; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import io.vertx.junit5.Timeout; |
| 16 | +import io.vertx.junit5.VertxTestContext; |
| 17 | +import jakarta.persistence.Column; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.FetchType; |
| 20 | +import jakarta.persistence.Id; |
| 21 | +import jakarta.persistence.IdClass; |
| 22 | +import jakarta.persistence.JoinColumn; |
| 23 | +import jakarta.persistence.ManyToOne; |
| 24 | +import jakarta.persistence.Table; |
| 25 | + |
| 26 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 30 | +public class ManyToOneLazyIdClassTest extends BaseReactiveTest { |
| 31 | + |
| 32 | + @Override |
| 33 | + protected Collection<Class<?>> annotatedEntities() { |
| 34 | + return List.of( Relationship.class, EntityA.class, EntityB.class ); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void test(VertxTestContext context) { |
| 39 | + final UUID aId = UUID.randomUUID(); |
| 40 | + final EntityA entityA = new EntityA( aId, "testA" ); |
| 41 | + final UUID bId = UUID.randomUUID(); |
| 42 | + final EntityB entityB = new EntityB( bId, "testB" ); |
| 43 | + final Relationship relationship = new Relationship( entityA, entityB, "testRelationship" ); |
| 44 | + |
| 45 | + test( context, getMutinySessionFactory() |
| 46 | + .withTransaction( s -> s.persistAll( entityA, entityB, relationship ) ) |
| 47 | + .chain( () -> getMutinySessionFactory().withTransaction( s -> s |
| 48 | + .find( EntityA.class, aId ) |
| 49 | + .invoke( entity -> assertThat( entity ).isEqualTo( entityA ) ) |
| 50 | + ) ) |
| 51 | + .chain( () -> getMutinySessionFactory().withTransaction( s -> s |
| 52 | + .find( EntityB.class, bId ) |
| 53 | + .invoke( entity -> assertThat( entity ).isEqualTo( entityB ) ) |
| 54 | + ) ) |
| 55 | + .chain( () -> getMutinySessionFactory().withTransaction( s -> s.find( |
| 56 | + Relationship.class, |
| 57 | + new RelationshipId( aId, bId ) |
| 58 | + ) ) ) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + @Entity |
| 63 | + @Table(name = "entity_a") |
| 64 | + public static class EntityA { |
| 65 | + |
| 66 | + @Id |
| 67 | + @Column(name = "id") |
| 68 | + private UUID id; |
| 69 | + |
| 70 | + @Column(name = "name") |
| 71 | + private String name; |
| 72 | + |
| 73 | + public EntityA(UUID id, String name) { |
| 74 | + this.id = id; |
| 75 | + this.name = name; |
| 76 | + } |
| 77 | + |
| 78 | + public EntityA() { |
| 79 | + } |
| 80 | + |
| 81 | + public UUID getId() { |
| 82 | + return id; |
| 83 | + } |
| 84 | + |
| 85 | + public void setId(UUID id) { |
| 86 | + this.id = id; |
| 87 | + } |
| 88 | + |
| 89 | + public String getName() { |
| 90 | + return name; |
| 91 | + } |
| 92 | + |
| 93 | + public void setName(String name) { |
| 94 | + this.name = name; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String toString() { |
| 99 | + return id + ":" + name; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object o) { |
| 104 | + if ( this == o ) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + if ( o == null || getClass() != o.getClass() ) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + EntityA entityA = (EntityA) o; |
| 111 | + return Objects.equals( id, entityA.id ) && Objects.equals( name, entityA.name ); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int hashCode() { |
| 116 | + return Objects.hash( id, name ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Entity |
| 121 | + @Table(name = "entity_b") |
| 122 | + public static class EntityB { |
| 123 | + |
| 124 | + @Id |
| 125 | + @Column(name = "id") |
| 126 | + private UUID id; |
| 127 | + |
| 128 | + @Column(name = "name") |
| 129 | + private String name; |
| 130 | + |
| 131 | + public EntityB(UUID id, String name) { |
| 132 | + this.id = id; |
| 133 | + this.name = name; |
| 134 | + } |
| 135 | + |
| 136 | + public EntityB() { |
| 137 | + } |
| 138 | + |
| 139 | + public UUID getId() { |
| 140 | + return id; |
| 141 | + } |
| 142 | + |
| 143 | + public void setId(UUID id) { |
| 144 | + this.id = id; |
| 145 | + } |
| 146 | + |
| 147 | + public String getName() { |
| 148 | + return name; |
| 149 | + } |
| 150 | + |
| 151 | + public void setName(String name) { |
| 152 | + this.name = name; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String toString() { |
| 157 | + return id + ":" + name; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean equals(Object o) { |
| 162 | + if ( this == o ) { |
| 163 | + return true; |
| 164 | + } |
| 165 | + if ( o == null || getClass() != o.getClass() ) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + EntityB entityB = (EntityB) o; |
| 169 | + return Objects.equals( id, entityB.id ) && Objects.equals( name, entityB.name ); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int hashCode() { |
| 174 | + return Objects.hash( id, name ); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Entity( name = "Relationship") |
| 179 | + @Table(name = "relationship") |
| 180 | + @IdClass(RelationshipId.class) |
| 181 | + public static class Relationship { |
| 182 | + |
| 183 | + @Id |
| 184 | + @ManyToOne(fetch = FetchType.LAZY) |
| 185 | + @JoinColumn(name = "a_id", referencedColumnName = "id") |
| 186 | + private EntityA entityA; |
| 187 | + |
| 188 | + @Id |
| 189 | + @ManyToOne(fetch = FetchType.LAZY) |
| 190 | + @JoinColumn(name = "b_id", referencedColumnName = "id") |
| 191 | + private EntityB entityB; |
| 192 | + |
| 193 | + @Column(name = "dataField") |
| 194 | + private String dataField; |
| 195 | + |
| 196 | + public Relationship(EntityA entityA, EntityB entityB, String dataField) { |
| 197 | + this.entityA = entityA; |
| 198 | + this.entityB = entityB; |
| 199 | + this.dataField = dataField; |
| 200 | + } |
| 201 | + |
| 202 | + public Relationship() { |
| 203 | + } |
| 204 | + |
| 205 | + public EntityA getEntityA() { |
| 206 | + return entityA; |
| 207 | + } |
| 208 | + |
| 209 | + public void setEntityA(EntityA entityA) { |
| 210 | + this.entityA = entityA; |
| 211 | + } |
| 212 | + |
| 213 | + public EntityB getEntityB() { |
| 214 | + return entityB; |
| 215 | + } |
| 216 | + |
| 217 | + public void setEntityB(EntityB entityB) { |
| 218 | + this.entityB = entityB; |
| 219 | + } |
| 220 | + |
| 221 | + public String getDataField() { |
| 222 | + return dataField; |
| 223 | + } |
| 224 | + |
| 225 | + public void setDataField(String dataField) { |
| 226 | + this.dataField = dataField; |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public String toString() { |
| 231 | + return entityA + "-" + entityB + ":" + dataField; |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public boolean equals(Object obj) { |
| 236 | + if ( this == obj ) { |
| 237 | + return true; |
| 238 | + } |
| 239 | + if ( !( obj instanceof Relationship ) ) { |
| 240 | + return false; |
| 241 | + } |
| 242 | + Relationship other = (Relationship) obj; |
| 243 | + if ( entityA == null |
| 244 | + || other.getEntityA() == null |
| 245 | + || entityB == null |
| 246 | + || other.getEntityB() == null |
| 247 | + ) { |
| 248 | + return false; |
| 249 | + } |
| 250 | + return entityA.equals( other.getEntityA() ) |
| 251 | + && entityB.equals( other.getEntityB() ); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public int hashCode() { |
| 256 | + return Objects.hash( entityA, entityB ); |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + public static class RelationshipId { |
| 261 | + |
| 262 | + private UUID entityA; |
| 263 | + private UUID entityB; |
| 264 | + |
| 265 | + public RelationshipId() { |
| 266 | + } |
| 267 | + |
| 268 | + public RelationshipId(UUID entityA, UUID entityB) { |
| 269 | + this.entityA = entityA; |
| 270 | + this.entityB = entityB; |
| 271 | + } |
| 272 | + |
| 273 | + public UUID getEntityA() { |
| 274 | + return entityA; |
| 275 | + } |
| 276 | + |
| 277 | + public void setEntityA(UUID entityA) { |
| 278 | + this.entityA = entityA; |
| 279 | + } |
| 280 | + |
| 281 | + public UUID getEntityB() { |
| 282 | + return entityB; |
| 283 | + } |
| 284 | + |
| 285 | + public void setEntityB(UUID entityB) { |
| 286 | + this.entityB = entityB; |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + public String toString() { |
| 291 | + return entityA + ":" + entityB; |
| 292 | + } |
| 293 | + |
| 294 | + @Override |
| 295 | + public boolean equals(Object obj) { |
| 296 | + if ( this == obj ) { |
| 297 | + return true; |
| 298 | + } |
| 299 | + if ( !( obj instanceof RelationshipId ) ) { |
| 300 | + return false; |
| 301 | + } |
| 302 | + RelationshipId other = (RelationshipId) obj; |
| 303 | + if ( entityA == null |
| 304 | + || other.getEntityA() == null |
| 305 | + || entityB == null |
| 306 | + || other.getEntityB() == null |
| 307 | + ) { |
| 308 | + return false; |
| 309 | + } |
| 310 | + return entityA.equals( other.getEntityA() ) |
| 311 | + && entityB.equals( other.getEntityB() ); |
| 312 | + } |
| 313 | + |
| 314 | + @Override |
| 315 | + public int hashCode() { |
| 316 | + return Objects.hash( entityA, entityB ); |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | +} |
0 commit comments