|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package x; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.FetchType; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.IdClass; |
| 11 | +import jakarta.persistence.OneToOne; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 21 | + |
| 22 | + |
| 23 | +@DomainModel( annotatedClasses = { |
| 24 | + IdClassSingleOneToOneTest.EntityA.class, |
| 25 | + IdClassSingleOneToOneTest.EntityB.class, |
| 26 | +} ) |
| 27 | +@SessionFactory |
| 28 | +public class IdClassSingleOneToOneTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void test(SessionFactoryScope scope) { |
| 32 | + scope.getSessionFactory(); |
| 33 | + scope.inTransaction( session -> { |
| 34 | + EntityA entityA = new EntityA(3); |
| 35 | + EntityB entityB = new EntityB( entityA ); |
| 36 | + entityA.entityB = entityB; |
| 37 | + session.persist( entityA ); |
| 38 | + session.persist( entityB ); |
| 39 | + assertEquals( new EntityBId(3), |
| 40 | + session.getIdentifier( entityB ) ); |
| 41 | + } ); |
| 42 | + scope.inTransaction( session -> { |
| 43 | + EntityB entityB = session.find( EntityB.class, new EntityBId(3) ); |
| 44 | + assertNotNull( entityB ); |
| 45 | + } ); |
| 46 | + } |
| 47 | + |
| 48 | + @Entity( name = "EntityA" ) |
| 49 | + static class EntityA { |
| 50 | + |
| 51 | + @Id |
| 52 | + private Integer id; |
| 53 | + |
| 54 | + @OneToOne( mappedBy = "entityA", fetch = FetchType.LAZY ) |
| 55 | + private EntityB entityB; |
| 56 | + |
| 57 | + public EntityA() { |
| 58 | + } |
| 59 | + |
| 60 | + public EntityA(Integer id) { |
| 61 | + this.id = id; |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @IdClass( EntityBId.class ) |
| 67 | + @Entity( name = "EntityB" ) |
| 68 | + static class EntityB { |
| 69 | + |
| 70 | + @Id |
| 71 | + @OneToOne( fetch = FetchType.LAZY ) |
| 72 | + private EntityA entityA; |
| 73 | + |
| 74 | + public EntityB() { |
| 75 | + } |
| 76 | + |
| 77 | + public EntityB(EntityA entityA) { |
| 78 | + this.entityA = entityA; |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + static class EntityBId { |
| 84 | + private final Integer entityA; |
| 85 | + |
| 86 | + EntityBId() { |
| 87 | + entityA = null; |
| 88 | + } |
| 89 | + EntityBId(Integer entityA) { |
| 90 | + this.entityA = entityA; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean equals(Object o) { |
| 95 | + return o instanceof EntityBId entityBId |
| 96 | + && Objects.equals( entityA, entityBId.entityA ); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int hashCode() { |
| 101 | + return Objects.hashCode( entityA ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments