|
| 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.Objects; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import io.vertx.junit5.Timeout; |
| 15 | +import io.vertx.junit5.VertxTestContext; |
| 16 | +import jakarta.persistence.Column; |
| 17 | +import jakarta.persistence.Embeddable; |
| 18 | +import jakarta.persistence.EmbeddedId; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.Table; |
| 21 | + |
| 22 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 26 | +public class EmbeddedIdTest extends BaseReactiveTest { |
| 27 | + |
| 28 | + @Override |
| 29 | + protected Collection<Class<?>> annotatedEntities() { |
| 30 | + return Set.of( Shipment.class ); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void test(VertxTestContext context) { |
| 35 | + ShipmentId id1 = new ShipmentId( "a", "b" ); |
| 36 | + Shipment shipment1 = new Shipment( id1, "test" ); |
| 37 | + |
| 38 | + ShipmentId id2 = new ShipmentId( "c", "d" ); |
| 39 | + Shipment shipment2 = new Shipment( id2, "test2" ); |
| 40 | + |
| 41 | + test( context, getMutinySessionFactory() |
| 42 | + .withTransaction( s -> s.persistAll( shipment1, shipment2 ) ) |
| 43 | + .chain( () -> getMutinySessionFactory().withTransaction( s -> s.find( Shipment.class, id1 ) ) ) |
| 44 | + .invoke( result -> assertThat( result ).isEqualTo( shipment1 ) ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Entity(name = "Shipment") |
| 49 | + @Table(name = "Shipment") |
| 50 | + public static class Shipment { |
| 51 | + |
| 52 | + @EmbeddedId |
| 53 | + private ShipmentId shipmentId; |
| 54 | + |
| 55 | + @Column(name = "field") |
| 56 | + private String field; |
| 57 | + |
| 58 | + public Shipment() { |
| 59 | + } |
| 60 | + |
| 61 | + public Shipment(ShipmentId shipmentId, String field) { |
| 62 | + this.shipmentId = shipmentId; |
| 63 | + this.field = field; |
| 64 | + } |
| 65 | + |
| 66 | + public ShipmentId getShipmentId() { |
| 67 | + return shipmentId; |
| 68 | + } |
| 69 | + |
| 70 | + public void setShipmentId(ShipmentId shipmentId) { |
| 71 | + this.shipmentId = shipmentId; |
| 72 | + } |
| 73 | + |
| 74 | + public String getField() { |
| 75 | + return field; |
| 76 | + } |
| 77 | + |
| 78 | + public void setField(String field) { |
| 79 | + this.field = field; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String toString() { |
| 84 | + return "Shipment{" + |
| 85 | + "shipmentId=" + shipmentId + |
| 86 | + ", field='" + field + '\'' + |
| 87 | + '}'; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object o) { |
| 92 | + if ( this == o ) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + if ( o == null || getClass() != o.getClass() ) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + Shipment table = (Shipment) o; |
| 99 | + return Objects.equals( shipmentId, table.shipmentId ) && |
| 100 | + Objects.equals( field, table.field ); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int hashCode() { |
| 105 | + return Objects.hash( shipmentId, field ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + @Embeddable |
| 111 | + public static class ShipmentId { |
| 112 | + |
| 113 | + @Column(name = "sp_country") |
| 114 | + private String country; |
| 115 | + |
| 116 | + @Column(name = "sp_city") |
| 117 | + private String city; |
| 118 | + |
| 119 | + public ShipmentId(String country, String city) { |
| 120 | + this.country = country; |
| 121 | + this.city = city; |
| 122 | + } |
| 123 | + |
| 124 | + public ShipmentId() { |
| 125 | + } |
| 126 | + |
| 127 | + public String getCountry() { |
| 128 | + return country; |
| 129 | + } |
| 130 | + |
| 131 | + public String getCity() { |
| 132 | + return city; |
| 133 | + } |
| 134 | + |
| 135 | + public void setCountry(String country) { |
| 136 | + this.country = country; |
| 137 | + } |
| 138 | + |
| 139 | + public void setCity(String city) { |
| 140 | + this.city = city; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String toString() { |
| 145 | + return "TableId{" + |
| 146 | + "p='" + country + '\'' + |
| 147 | + ", t='" + city + '\'' + |
| 148 | + '}'; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean equals(Object o) { |
| 153 | + if ( this == o ) { |
| 154 | + return true; |
| 155 | + } |
| 156 | + if ( o == null || getClass() != o.getClass() ) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + ShipmentId tableId = (ShipmentId) o; |
| 160 | + return Objects.equals( country, tableId.country ) && |
| 161 | + Objects.equals( city, tableId.city ); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int hashCode() { |
| 166 | + return Objects.hash( country, city ); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments