|
| 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 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import io.vertx.junit5.Timeout; |
| 15 | +import io.vertx.junit5.VertxTestContext; |
| 16 | +import jakarta.persistence.CascadeType; |
| 17 | +import jakarta.persistence.Column; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.FetchType; |
| 20 | +import jakarta.persistence.GeneratedValue; |
| 21 | +import jakarta.persistence.GenerationType; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.MapsId; |
| 24 | +import jakarta.persistence.OneToOne; |
| 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 OneToOneMapsIdAndGeneratedIdTest extends BaseReactiveTest { |
| 31 | + |
| 32 | + @Override |
| 33 | + protected Collection<Class<?>> annotatedEntities() { |
| 34 | + return List.of( Person.class, NaturalPerson.class ); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testPersist(VertxTestContext context) { |
| 39 | + NaturalPerson naturalPerson = new NaturalPerson( "natual" ); |
| 40 | + Person person = new Person( "person", naturalPerson ); |
| 41 | + |
| 42 | + test( |
| 43 | + context, getMutinySessionFactory() |
| 44 | + .withTransaction( session -> session.persist( person ) ) |
| 45 | + .chain( () -> getMutinySessionFactory() |
| 46 | + .withTransaction( session -> session |
| 47 | + .find( Person.class, person.getId() ) |
| 48 | + .invoke( result -> { |
| 49 | + assertThat( result ).isNotNull(); |
| 50 | + assertThat( result.getNaturalPerson() ).isNotNull(); |
| 51 | + assertThat( result.getNaturalPerson().getId() ).isEqualTo( result.getId() ); |
| 52 | + } ) ) |
| 53 | + ) |
| 54 | + ); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Entity |
| 59 | + public static class Person { |
| 60 | + |
| 61 | + @Id |
| 62 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 63 | + private Long id; |
| 64 | + |
| 65 | + private String name; |
| 66 | + |
| 67 | + @OneToOne(mappedBy = "person", cascade = CascadeType.ALL) |
| 68 | + private NaturalPerson naturalPerson; |
| 69 | + |
| 70 | + public Person() { |
| 71 | + } |
| 72 | + |
| 73 | + public Person(String name, NaturalPerson naturalPerson) { |
| 74 | + this.name = name; |
| 75 | + this.naturalPerson = naturalPerson; |
| 76 | + naturalPerson.person = this; |
| 77 | + } |
| 78 | + |
| 79 | + public Long getId() { |
| 80 | + return id; |
| 81 | + } |
| 82 | + |
| 83 | + public String getName() { |
| 84 | + return name; |
| 85 | + } |
| 86 | + |
| 87 | + public NaturalPerson getNaturalPerson() { |
| 88 | + return naturalPerson; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object o) { |
| 93 | + if ( o == null || getClass() != o.getClass() ) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + Person person = (Person) o; |
| 97 | + return Objects.equals( name, person.name ); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int hashCode() { |
| 102 | + return Objects.hashCode( name ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Entity |
| 107 | + public static class NaturalPerson { |
| 108 | + |
| 109 | + @Id |
| 110 | + private Long id; |
| 111 | + |
| 112 | + @Column |
| 113 | + private String name; |
| 114 | + |
| 115 | + @OneToOne(fetch = FetchType.LAZY) |
| 116 | + @MapsId |
| 117 | + private Person person; |
| 118 | + |
| 119 | + public NaturalPerson() { |
| 120 | + } |
| 121 | + |
| 122 | + public NaturalPerson(String name) { |
| 123 | + this.name = name; |
| 124 | + } |
| 125 | + |
| 126 | + public Long getId() { |
| 127 | + return id; |
| 128 | + } |
| 129 | + |
| 130 | + public String getName() { |
| 131 | + return name; |
| 132 | + } |
| 133 | + |
| 134 | + public Person getPerson() { |
| 135 | + return person; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean equals(Object o) { |
| 140 | + if ( o == null || getClass() != o.getClass() ) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + NaturalPerson that = (NaturalPerson) o; |
| 144 | + return Objects.equals( name, that.name ); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public int hashCode() { |
| 149 | + return Objects.hashCode( name ); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | +} |
0 commit comments