|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.annotations.derivedidentities; |
| 6 | + |
| 7 | +import jakarta.persistence.Column; |
| 8 | +import jakarta.persistence.Embeddable; |
| 9 | +import jakarta.persistence.EmbeddedId; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.JoinColumn; |
| 13 | +import jakarta.persistence.ManyToOne; |
| 14 | +import jakarta.persistence.OneToMany; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.math.BigDecimal; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +@JiraKey("HHH-18702") |
| 29 | +@DomainModel( |
| 30 | + annotatedClasses = { |
| 31 | + OneToManyEmbeddableId.Parent.class, |
| 32 | + OneToManyEmbeddableId.FirstChild.class, |
| 33 | + OneToManyEmbeddableId.SecondChild.class, |
| 34 | + } |
| 35 | +) |
| 36 | +@SessionFactory |
| 37 | +public class OneToManyEmbeddableId { |
| 38 | + private static final BigDecimal FIRST_CHILD_CODE = new BigDecimal( 2 ); |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + public static void init(SessionFactoryScope scope) { |
| 42 | + scope.inTransaction( |
| 43 | + session -> { |
| 44 | + Parent parent = new Parent( BigDecimal.TEN, "Lio" ); |
| 45 | + FirstChild firstChild = new FirstChild( parent, BigDecimal.ONE, FIRST_CHILD_CODE ); |
| 46 | + SecondChild secondChild = new SecondChild( firstChild, BigDecimal.TEN, "Al" ); |
| 47 | + firstChild.addChild( secondChild ); |
| 48 | + session.persist( parent ); |
| 49 | + session.persist( firstChild ); |
| 50 | + session.persist( secondChild ); |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void propertyNavigationTest(SessionFactoryScope scope) { |
| 57 | + scope.inTransaction( |
| 58 | + session -> { |
| 59 | + FirstChild firstChild = session.createQuery( |
| 60 | + "select f from FirstChild f where f.code = :code", FirstChild.class ) |
| 61 | + .setParameter( "code", FIRST_CHILD_CODE ).getSingleResult(); |
| 62 | + |
| 63 | + assertThat( firstChild ).isNotNull(); |
| 64 | + |
| 65 | + assertThat( firstChild.getChildren() ).hasSize( 1 ); |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Entity(name = "TestEntity") |
| 72 | + public static class Parent { |
| 73 | + @Id |
| 74 | + private BigDecimal id; |
| 75 | + |
| 76 | + private String name; |
| 77 | + |
| 78 | + public Parent() { |
| 79 | + } |
| 80 | + |
| 81 | + public Parent(BigDecimal id, String name) { |
| 82 | + this.id = id; |
| 83 | + this.name = name; |
| 84 | + } |
| 85 | + |
| 86 | + @OneToMany(mappedBy = "id.parent") |
| 87 | + private List<FirstChild> children = new ArrayList<>(); |
| 88 | + |
| 89 | + void addChild(FirstChild firstChild) { |
| 90 | + children.add( firstChild ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Entity(name = "FirstChild") |
| 95 | + public static class FirstChild { |
| 96 | + @EmbeddedId |
| 97 | + private FirstChildId id; |
| 98 | + |
| 99 | + @Column(name = "FIRST_CHILD_CODE") |
| 100 | + private BigDecimal code; |
| 101 | + |
| 102 | + @OneToMany(mappedBy = "id.firstChild") |
| 103 | + private List<SecondChild> children = new ArrayList<>(); |
| 104 | + |
| 105 | + public FirstChildId getId() { |
| 106 | + return id; |
| 107 | + } |
| 108 | + |
| 109 | + public FirstChild() { |
| 110 | + } |
| 111 | + |
| 112 | + public FirstChild(Parent parent, BigDecimal bigDecimalNum, BigDecimal code) { |
| 113 | + this.id = new FirstChildId( bigDecimalNum, parent ); |
| 114 | + parent.addChild( this ); |
| 115 | + this.code = code; |
| 116 | + } |
| 117 | + |
| 118 | + public List<SecondChild> getChildren() { |
| 119 | + return children; |
| 120 | + } |
| 121 | + |
| 122 | + void addChild(SecondChild child) { |
| 123 | + children.add( child ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Embeddable |
| 128 | + public static class FirstChildId { |
| 129 | + private BigDecimal bigDecimalNum; |
| 130 | + |
| 131 | + @ManyToOne() |
| 132 | + private Parent parent; |
| 133 | + |
| 134 | + public FirstChildId() { |
| 135 | + } |
| 136 | + |
| 137 | + public FirstChildId(BigDecimal bigDecimalNum, Parent parent) { |
| 138 | + this.bigDecimalNum = bigDecimalNum; |
| 139 | + this.parent = parent; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Entity(name = "SecondChild") |
| 144 | + public static class SecondChild { |
| 145 | + @EmbeddedId |
| 146 | + private SecondChildId id; |
| 147 | + |
| 148 | + private String name; |
| 149 | + |
| 150 | + public SecondChild() { |
| 151 | + } |
| 152 | + |
| 153 | + public SecondChild(FirstChild firstChild, BigDecimal bigDecimalNum, String name) { |
| 154 | + this.id = new SecondChildId( bigDecimalNum, firstChild ); |
| 155 | + this.name = name; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Embeddable |
| 160 | + public static class SecondChildId { |
| 161 | + private BigDecimal bigDecimalNum; |
| 162 | + |
| 163 | + @ManyToOne |
| 164 | + @JoinColumn(name = "FIRST_CHILD_CODE", referencedColumnName = "FIRST_CHILD_CODE") |
| 165 | + private FirstChild firstChild; |
| 166 | + |
| 167 | + public SecondChildId() { |
| 168 | + } |
| 169 | + |
| 170 | + public SecondChildId(BigDecimal bigDecimalNum, FirstChild firstChild) { |
| 171 | + this.bigDecimalNum = bigDecimalNum; |
| 172 | + this.firstChild = firstChild; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments