|
| 1 | +package org.hibernate.orm.test.annotations.embeddables; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 6 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 7 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import jakarta.persistence.Column; |
| 12 | +import jakarta.persistence.DiscriminatorColumn; |
| 13 | +import jakarta.persistence.Embeddable; |
| 14 | +import jakarta.persistence.Entity; |
| 15 | +import jakarta.persistence.Id; |
| 16 | +import jakarta.persistence.Inheritance; |
| 17 | +import jakarta.persistence.InheritanceType; |
| 18 | +import jakarta.persistence.MappedSuperclass; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +@DomainModel( |
| 23 | + annotatedClasses = { |
| 24 | + EmbeddableWithGenericAndMappedSuperClassTest.PopularBook.class, |
| 25 | + EmbeddableWithGenericAndMappedSuperClassTest.RareBook.class |
| 26 | + } |
| 27 | +) |
| 28 | +@SessionFactory |
| 29 | +public class EmbeddableWithGenericAndMappedSuperClassTest { |
| 30 | + |
| 31 | + private final static long POPULAR_BOOK_ID = 1l; |
| 32 | + private final static String POPULAR_BOOK_CODE = "POP"; |
| 33 | + private final static long RARE_BOOK_ID = 2l; |
| 34 | + private final static Integer RARE_BOOK_CODE = 123; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + session -> { |
| 40 | + Edition popularEdition = new Edition<>( "Popular", POPULAR_BOOK_CODE ); |
| 41 | + PopularBook popularBook = new PopularBook( POPULAR_BOOK_ID, popularEdition ); |
| 42 | + |
| 43 | + Edition rareEdition = new Edition<>( "Rare", RARE_BOOK_CODE ); |
| 44 | + RareBook rareBook = new RareBook( RARE_BOOK_ID, rareEdition ); |
| 45 | + |
| 46 | + session.persist( popularBook ); |
| 47 | + session.persist( rareBook ); |
| 48 | + } |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void test(SessionFactoryScope scope) { |
| 54 | + scope.inTransaction( |
| 55 | + session -> { |
| 56 | + List<Integer> rareBookCodes = session.createQuery( |
| 57 | + "select b.edition.code from RareBook b where b.id = :id", |
| 58 | + Integer.class |
| 59 | + ).setParameter( "id", RARE_BOOK_ID ).list(); |
| 60 | + |
| 61 | + assertThat( rareBookCodes.size() ).isEqualTo( 1 ); |
| 62 | + |
| 63 | + Integer code = rareBookCodes.get( 0 ); |
| 64 | + assertThat( code ).isEqualTo( RARE_BOOK_CODE ); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + scope.inTransaction( |
| 69 | + session -> { |
| 70 | + List<Integer> rareBookCodes = session.createQuery( |
| 71 | + "select c.code from Base b left join treat(b as RareBook).edition c where b.id = :id", |
| 72 | + Integer.class |
| 73 | + ).setParameter( "id", RARE_BOOK_ID ).list(); |
| 74 | + |
| 75 | + assertThat( rareBookCodes.size() ).isEqualTo( 1 ); |
| 76 | + |
| 77 | + Integer code = rareBookCodes.get( 0 ); |
| 78 | + assertThat( code ).isEqualTo( RARE_BOOK_CODE ); |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + scope.inTransaction( |
| 83 | + session -> { |
| 84 | + List<String> populareBookCodes = session.createQuery( |
| 85 | + "select b.edition.code from PopularBook b where b.id = :id", |
| 86 | + String.class |
| 87 | + ).setParameter( "id", POPULAR_BOOK_ID ).list(); |
| 88 | + |
| 89 | + assertThat( populareBookCodes.size() ).isEqualTo( 1 ); |
| 90 | + |
| 91 | + String code = populareBookCodes.get( 0 ); |
| 92 | + assertThat( code ).isEqualTo( POPULAR_BOOK_CODE ); |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + scope.inTransaction( |
| 97 | + session -> { |
| 98 | + List<String> rareBookCodes = session.createQuery( |
| 99 | + "select c.code from Base b left join treat(b as PopularBook).edition c where b.id = :id", |
| 100 | + String.class |
| 101 | + ).setParameter( "id", POPULAR_BOOK_ID ).list(); |
| 102 | + |
| 103 | + assertThat( rareBookCodes.size() ).isEqualTo( 1 ); |
| 104 | + |
| 105 | + String code = rareBookCodes.get( 0 ); |
| 106 | + assertThat( code ).isEqualTo( POPULAR_BOOK_CODE ); |
| 107 | + } |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + @Embeddable |
| 112 | + public static class Edition<T> { |
| 113 | + private String editorName; |
| 114 | + |
| 115 | + @Column(name = "CODE_COLUMN") |
| 116 | + private T code; |
| 117 | + |
| 118 | + public Edition() { |
| 119 | + } |
| 120 | + |
| 121 | + public Edition(String editorName, T code) { |
| 122 | + this.editorName = editorName; |
| 123 | + this.code = code; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Entity(name = "Base") |
| 128 | + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
| 129 | + @DiscriminatorColumn(name = "PROP_TYPE") |
| 130 | + public abstract static class Base { |
| 131 | + @Id |
| 132 | + private Long id; |
| 133 | + |
| 134 | + |
| 135 | + public Base() { |
| 136 | + } |
| 137 | + |
| 138 | + public Base(Long id) { |
| 139 | + this.id = id; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @MappedSuperclass |
| 144 | + public static abstract class Book<T> extends Base { |
| 145 | + private Edition<T> edition; |
| 146 | + |
| 147 | + public Book() { |
| 148 | + } |
| 149 | + |
| 150 | + public Book(Long id, Edition<T> edition) { |
| 151 | + super( id ); |
| 152 | + this.edition = edition; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + @Entity(name = "PopularBook") |
| 158 | + public static class PopularBook extends Book<String> { |
| 159 | + |
| 160 | + |
| 161 | + public PopularBook() { |
| 162 | + } |
| 163 | + |
| 164 | + public PopularBook(Long id, Edition<String> edition) { |
| 165 | + super( id, edition ); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Entity(name = "RareBook") |
| 170 | + public static class RareBook extends Book<Integer> { |
| 171 | + |
| 172 | + public RareBook() { |
| 173 | + } |
| 174 | + |
| 175 | + public RareBook(Long id, Edition<Integer> edition) { |
| 176 | + super( id, edition ); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | +} |
0 commit comments