From 23e13573961916235261676e786e5d7bff822754 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 12 Sep 2024 14:45:20 +0200 Subject: [PATCH] [#1979] Test find with @EmbeddedId --- .../hibernate/reactive/EmbeddedIdTest.java | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 hibernate-reactive-core/src/test/java/org/hibernate/reactive/EmbeddedIdTest.java diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EmbeddedIdTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EmbeddedIdTest.java new file mode 100644 index 000000000..de3590fab --- /dev/null +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EmbeddedIdTest.java @@ -0,0 +1,174 @@ +/* Hibernate, Relational Persistence for Idiomatic Java + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright: Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.reactive; + +import java.util.Collection; +import java.util.Objects; +import java.util.Set; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import io.vertx.junit5.Timeout; +import io.vertx.junit5.VertxTestContext; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static org.assertj.core.api.Assertions.assertThat; + +@Timeout(value = 10, timeUnit = MINUTES) +public class EmbeddedIdTest extends BaseReactiveTest { + + @Override + protected Collection> annotatedEntities() { + return Set.of( Delivery.class ); + } + + LocationId verbania = new LocationId( "Italy", "Verbania" ); + Delivery pizza = new Delivery( verbania, "Pizza Margherita" ); + + LocationId hallein = new LocationId( "Austria", "Hallein" ); + Delivery schnitzel = new Delivery( hallein, "Wiener Schnitzel" ); + + @BeforeEach + public void populateDb(VertxTestContext context) { + test( context, getMutinySessionFactory().withTransaction( s -> s.persistAll( pizza, schnitzel ) ) ); + } + + @Test + public void testFindSingleId(VertxTestContext context) { + test( context, getMutinySessionFactory().withTransaction( s -> s.find( Delivery.class, verbania ) ) + .invoke( result -> assertThat( result ).isEqualTo( pizza ) ) + ); + } + + @Test + public void testFindMultipleIds(VertxTestContext context) { + test( context, getMutinySessionFactory().withTransaction( s -> s.find( Delivery.class, verbania, hallein ) ) + .invoke( result -> assertThat( result ).containsExactlyInAnyOrder( pizza, schnitzel ) ) + ); + } + + @Entity(name = "Delivery") + @Table(name = "Delivery") + public static class Delivery { + + @EmbeddedId + private LocationId locationId; + + @Column(name = "field") + private String field; + + public Delivery() { + } + + public Delivery(LocationId locationId, String field) { + this.locationId = locationId; + this.field = field; + } + + public LocationId getLocationId() { + return locationId; + } + + public void setLocationId(LocationId locationId) { + this.locationId = locationId; + } + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } + + @Override + public String toString() { + return locationId + ":" + field; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Delivery table = (Delivery) o; + return Objects.equals( locationId, table.locationId ) && + Objects.equals( field, table.field ); + } + + @Override + public int hashCode() { + return Objects.hash( locationId, field ); + } + } + + + @Embeddable + public static class LocationId { + + @Column(name = "sp_country") + private String country; + + @Column(name = "sp_city") + private String city; + + public LocationId(String country, String city) { + this.country = country; + this.city = city; + } + + public LocationId() { + } + + public String getCountry() { + return country; + } + + public String getCity() { + return city; + } + + public void setCountry(String country) { + this.country = country; + } + + public void setCity(String city) { + this.city = city; + } + + @Override + public String toString() { + return "[" + country + "-" + city + "]"; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + LocationId tableId = (LocationId) o; + return Objects.equals( country, tableId.country ) && + Objects.equals( city, tableId.city ); + } + + @Override + public int hashCode() { + return Objects.hash( country, city ); + } + } +}