|
| 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 org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import io.vertx.junit5.VertxTestContext; |
| 11 | +import jakarta.persistence.Embeddable; |
| 12 | +import jakarta.persistence.EmbeddedId; |
| 13 | +import jakarta.persistence.Entity; |
| 14 | +import jakarta.persistence.FetchType; |
| 15 | +import jakarta.persistence.Id; |
| 16 | +import jakarta.persistence.JoinColumn; |
| 17 | +import jakarta.persistence.OneToOne; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +public class EmbeddedIdWithOneToOneTest extends BaseReactiveTest { |
| 25 | + |
| 26 | + @Override |
| 27 | + protected Collection<Class<?>> annotatedEntities() { |
| 28 | + return List.of( FooEntity.class, BarEntity.class ); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void test(VertxTestContext context) { |
| 33 | + BarEntity barEntity = new BarEntity( "1" ); |
| 34 | + FooId fooId = new FooId( barEntity ); |
| 35 | + FooEntity entity = new FooEntity( fooId ); |
| 36 | + |
| 37 | + test( |
| 38 | + context, getMutinySessionFactory() |
| 39 | + .withTransaction( s -> s.persistAll( barEntity, entity ) ) |
| 40 | + .chain( () -> getMutinySessionFactory() |
| 41 | + .withTransaction( s -> s.find( FooEntity.class, fooId ) ) |
| 42 | + ) |
| 43 | + .invoke( result -> { |
| 44 | + assertThat( result.getId() ).isEqualTo( fooId ); |
| 45 | + assertThat( result.getId().getIdEntity() ).isEqualTo( fooId.getIdEntity() ); |
| 46 | + assertThat( result.getId().getIdEntity().getId() ).isEqualTo( fooId.getIdEntity().getId() ); |
| 47 | + } ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Entity(name = "bar") |
| 52 | + public static class BarEntity { |
| 53 | + |
| 54 | + @Id |
| 55 | + private String id; |
| 56 | + |
| 57 | + public BarEntity() { |
| 58 | + } |
| 59 | + |
| 60 | + public BarEntity(String id) { |
| 61 | + this.id = id; |
| 62 | + } |
| 63 | + |
| 64 | + public String getId() { |
| 65 | + return id; |
| 66 | + } |
| 67 | + |
| 68 | + public void setId(String id) { |
| 69 | + this.id = id; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Entity(name = "foo") |
| 74 | + public static class FooEntity { |
| 75 | + |
| 76 | + @EmbeddedId |
| 77 | + private FooId id; |
| 78 | + |
| 79 | + public FooEntity() { |
| 80 | + } |
| 81 | + |
| 82 | + public FooEntity(FooId id) { |
| 83 | + this.id = id; |
| 84 | + } |
| 85 | + |
| 86 | + public FooId getId() { |
| 87 | + return id; |
| 88 | + } |
| 89 | + |
| 90 | + public void setId(FooId id) { |
| 91 | + this.id = id; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Embeddable |
| 96 | + public static class FooId { |
| 97 | + |
| 98 | + @OneToOne(fetch = FetchType.EAGER) |
| 99 | + @JoinColumn(name = "id", nullable = false) |
| 100 | + private BarEntity barEntity; |
| 101 | + |
| 102 | + public FooId() { |
| 103 | + } |
| 104 | + |
| 105 | + public FooId(BarEntity barEntity) { |
| 106 | + this.barEntity = barEntity; |
| 107 | + } |
| 108 | + |
| 109 | + public BarEntity getIdEntity() { |
| 110 | + return barEntity; |
| 111 | + } |
| 112 | + |
| 113 | + public void setIdEntity(BarEntity barEntity) { |
| 114 | + this.barEntity = barEntity; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean equals(Object o) { |
| 119 | + if ( o == null || getClass() != o.getClass() ) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + FooId fooId = (FooId) o; |
| 123 | + return Objects.equals( barEntity, fooId.barEntity ); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hashCode( barEntity ); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments