|
| 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.Timeout; |
| 11 | +import io.vertx.junit5.VertxTestContext; |
| 12 | +import jakarta.persistence.Entity; |
| 13 | +import jakarta.persistence.GeneratedValue; |
| 14 | +import jakarta.persistence.Id; |
| 15 | +import jakarta.persistence.IdClass; |
| 16 | +import jakarta.persistence.SequenceGenerator; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 25 | + |
| 26 | +public class CompositeIdWithGeneratedValuesTest extends BaseReactiveTest { |
| 27 | + |
| 28 | + @Override |
| 29 | + protected Collection<Class<?>> annotatedEntities() { |
| 30 | + return List.of( Product.class ); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCompositeIdWithGeneratedValues(VertxTestContext context) { |
| 35 | + final Product product = new Product( "name", 1150L ); |
| 36 | + test( |
| 37 | + context, |
| 38 | + getMutinySessionFactory().withTransaction( session -> session.persist( product ) ) |
| 39 | + .invoke( () -> assertThat( product.id ).isNotNull() ) |
| 40 | + .chain( () -> getMutinySessionFactory().withTransaction( session -> session.find( |
| 41 | + Product.class, |
| 42 | + new ProductId( product.version, product.id ) |
| 43 | + ) ) ) |
| 44 | + .invoke( found -> { |
| 45 | + assertThat( found ).hasFieldOrPropertyWithValue( "id", product.id ); |
| 46 | + assertThat( found ).hasFieldOrPropertyWithValue( "version", product.version ); |
| 47 | + assertThat( found ).hasFieldOrPropertyWithValue( "name", product.name ); |
| 48 | + } ) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Entity |
| 53 | + @IdClass(ProductId.class) |
| 54 | + public static class Product { |
| 55 | + @Id |
| 56 | + private Long version; |
| 57 | + |
| 58 | + @Id |
| 59 | + @GeneratedValue |
| 60 | + @SequenceGenerator(name = "product_seq", sequenceName = "product_seq") |
| 61 | + private Long id; |
| 62 | + |
| 63 | + private String name; |
| 64 | + |
| 65 | + public Product() { |
| 66 | + } |
| 67 | + |
| 68 | + public Product(String name, Long version) { |
| 69 | + this.name = name; |
| 70 | + this.version = version; |
| 71 | + } |
| 72 | + |
| 73 | + public Long getVersion() { |
| 74 | + return version; |
| 75 | + } |
| 76 | + |
| 77 | + public void setVersion(Long version) { |
| 78 | + this.version = version; |
| 79 | + } |
| 80 | + |
| 81 | + public Long getId() { |
| 82 | + return id; |
| 83 | + } |
| 84 | + |
| 85 | + public String getName() { |
| 86 | + return name; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String toString() { |
| 91 | + return "(" + id + "," + version + "):" + name; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static class ProductId { |
| 96 | + private Long version; |
| 97 | + private Long id; |
| 98 | + |
| 99 | + private ProductId() { |
| 100 | + } |
| 101 | + |
| 102 | + public ProductId(Long version, Long id) { |
| 103 | + this.version = version; |
| 104 | + this.id = id; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments