|
| 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.embeddables; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import jakarta.persistence.Column; |
| 18 | +import jakarta.persistence.Embeddable; |
| 19 | +import jakarta.persistence.Embedded; |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.Inheritance; |
| 23 | +import jakarta.persistence.InheritanceType; |
| 24 | +import jakarta.persistence.MappedSuperclass; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +@DomainModel( |
| 29 | + annotatedClasses = { |
| 30 | + GenericEmbeddableWthSubclassTest.PopularBook.class, |
| 31 | + GenericEmbeddableWthSubclassTest.RareBook.class, |
| 32 | + } |
| 33 | +) |
| 34 | +@SessionFactory |
| 35 | +@JiraKey( "HHH-15560") |
| 36 | +public class GenericEmbeddableWthSubclassTest { |
| 37 | + private final static long POPULAR_BOOK_ID = 1l; |
| 38 | + private final static String POPULAR_BOOK_CODE = "POP"; |
| 39 | + private final static Integer POPULAR_BOOK_YEAR = 1971; |
| 40 | + private final static long RARE_BOOK_ID = 2l; |
| 41 | + private final static Integer RARE_BOOK_CODE = 123; |
| 42 | + private final static String RARE_BOOK_STATE = "Good"; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setUp(SessionFactoryScope scope) { |
| 46 | + scope.inTransaction( |
| 47 | + session -> { |
| 48 | + PopularEdition popularEdition = new PopularEdition( |
| 49 | + "Popular", |
| 50 | + POPULAR_BOOK_CODE, |
| 51 | + POPULAR_BOOK_YEAR |
| 52 | + ); |
| 53 | + PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition ); |
| 54 | + |
| 55 | + RareEdition rareEdition = new RareEdition( "Rare", RARE_BOOK_CODE, RARE_BOOK_STATE ); |
| 56 | + RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition ); |
| 57 | + |
| 58 | + session.persist( popularBook ); |
| 59 | + session.persist( rareBook ); |
| 60 | + } |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterEach |
| 65 | + public void tearDown(SessionFactoryScope scope) { |
| 66 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testSelectSpecificEmbeddedAttribute(SessionFactoryScope scope) { |
| 71 | + scope.inTransaction( |
| 72 | + session -> { |
| 73 | + List<String> rareBookCodes = session.createQuery( |
| 74 | + "select b.edition.state from RareBook b where b.id = :id", |
| 75 | + String.class |
| 76 | + ).setParameter( "id", RARE_BOOK_ID ).list(); |
| 77 | + |
| 78 | + assertThat( rareBookCodes.size() ).isEqualTo( 1 ); |
| 79 | + |
| 80 | + String code = rareBookCodes.get( 0 ); |
| 81 | + assertThat( code ).isEqualTo( RARE_BOOK_STATE ); |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + scope.inTransaction( |
| 86 | + session -> { |
| 87 | + List<Integer> populareBookCodes = session.createQuery( |
| 88 | + "select b.edition.year from PopularBook b where b.id = :id", |
| 89 | + Integer.class |
| 90 | + ).setParameter( "id", POPULAR_BOOK_ID ).list(); |
| 91 | + |
| 92 | + assertThat( populareBookCodes.size() ).isEqualTo( 1 ); |
| 93 | + |
| 94 | + Integer code = populareBookCodes.get( 0 ); |
| 95 | + assertThat( code ).isEqualTo( POPULAR_BOOK_YEAR ); |
| 96 | + } |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testSelectBaseEmbeddableAttribute(SessionFactoryScope scope) { |
| 102 | + scope.inTransaction( |
| 103 | + session -> { |
| 104 | + List<Integer> rareBookCodes = session.createQuery( |
| 105 | + "select b.edition.code from RareBook b where b.id = :id", |
| 106 | + Integer.class |
| 107 | + ).setParameter( "id", RARE_BOOK_ID ).list(); |
| 108 | + |
| 109 | + assertThat( rareBookCodes.size() ).isEqualTo( 1 ); |
| 110 | + |
| 111 | + Integer code = rareBookCodes.get( 0 ); |
| 112 | + assertThat( code ).isEqualTo( RARE_BOOK_CODE ); |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + scope.inTransaction( |
| 117 | + session -> { |
| 118 | + List<String> populareBookCodes = session.createQuery( |
| 119 | + "select b.edition.code from PopularBook b where b.id = :id", |
| 120 | + String.class |
| 121 | + ).setParameter( "id", POPULAR_BOOK_ID ).list(); |
| 122 | + |
| 123 | + assertThat( populareBookCodes.size() ).isEqualTo( 1 ); |
| 124 | + |
| 125 | + String code = populareBookCodes.get( 0 ); |
| 126 | + assertThat( code ).isEqualTo( POPULAR_BOOK_CODE ); |
| 127 | + } |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + @MappedSuperclass |
| 132 | + public static class Edition<T> { |
| 133 | + private String editorName; |
| 134 | + |
| 135 | + @Column(name = "CODE_COLUMN") |
| 136 | + private T code; |
| 137 | + |
| 138 | + public Edition() { |
| 139 | + } |
| 140 | + |
| 141 | + public Edition(String editorName, T code) { |
| 142 | + this.editorName = editorName; |
| 143 | + this.code = code; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Embeddable |
| 148 | + public static class RareEdition extends Edition<Integer> { |
| 149 | + |
| 150 | + private String state; |
| 151 | + |
| 152 | + public RareEdition() { |
| 153 | + } |
| 154 | + |
| 155 | + public RareEdition(String editorName, Integer code, String state) { |
| 156 | + super( editorName, code ); |
| 157 | + this.state = state; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Embeddable |
| 162 | + public static class PopularEdition extends Edition<String> { |
| 163 | + |
| 164 | + @Column(name = "YEAR_COLUMN") |
| 165 | + private Integer year; |
| 166 | + |
| 167 | + public PopularEdition() { |
| 168 | + } |
| 169 | + |
| 170 | + public PopularEdition(String editorName, String code, Integer year) { |
| 171 | + super( editorName, code ); |
| 172 | + this.year = year; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + @Entity(name = "Base") |
| 178 | + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
| 179 | + public static class Base { |
| 180 | + @Id |
| 181 | + private Long id; |
| 182 | + |
| 183 | + public Base() { |
| 184 | + } |
| 185 | + |
| 186 | + public Base(Long id) { |
| 187 | + this.id = id; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + @MappedSuperclass |
| 193 | + public static abstract class Book<T extends Edition> extends Base { |
| 194 | + @Embedded |
| 195 | + private T edition; |
| 196 | + |
| 197 | + public Book() { |
| 198 | + } |
| 199 | + |
| 200 | + public Book(Long id, T edition) { |
| 201 | + super( id ); |
| 202 | + this.edition = edition; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | + @Entity(name = "PopularBook") |
| 208 | + public static class PopularBook extends Book<PopularEdition> { |
| 209 | + |
| 210 | + public PopularBook() { |
| 211 | + } |
| 212 | + |
| 213 | + public PopularBook(Long id, PopularEdition edition) { |
| 214 | + super( id, edition ); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + @Entity(name = "RareBook") |
| 219 | + public static class RareBook extends Book<RareEdition> { |
| 220 | + |
| 221 | + public RareBook() { |
| 222 | + } |
| 223 | + |
| 224 | + public RareBook(Long id, RareEdition edition) { |
| 225 | + super( id, edition ); |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | +} |
0 commit comments