|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.metamodel.mapping; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import javax.persistence.DiscriminatorColumn; |
| 11 | +import javax.persistence.DiscriminatorValue; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.Id; |
| 14 | +import javax.persistence.Inheritance; |
| 15 | +import javax.persistence.InheritanceType; |
| 16 | +import javax.persistence.Table; |
| 17 | + |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 27 | +import static org.hamcrest.CoreMatchers.is; |
| 28 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Andrea Boriero |
| 33 | + */ |
| 34 | +@DomainModel( |
| 35 | + annotatedClasses = { |
| 36 | + MixedInheritanceTest.Customer.class, |
| 37 | + MixedInheritanceTest.DomesticCustomer.class, |
| 38 | + MixedInheritanceTest.ForeignCustomer.class, |
| 39 | + MixedInheritanceTest.Person.class |
| 40 | + } |
| 41 | +) |
| 42 | +@ServiceRegistry |
| 43 | +@SessionFactory |
| 44 | +public class MixedInheritanceTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void rootQueryExecutionTest(SessionFactoryScope scope) { |
| 48 | + scope.inTransaction( |
| 49 | + session -> { |
| 50 | + { |
| 51 | + // [name, taxId, vat] |
| 52 | + final List<Customer> results = session.createQuery( |
| 53 | + "select c from Customer c", |
| 54 | + Customer.class |
| 55 | + ).list(); |
| 56 | + |
| 57 | + assertThat( results.size(), is( 3 ) ); |
| 58 | + |
| 59 | + for ( Customer result : results ) { |
| 60 | + if ( result.getId() == 1 ) { |
| 61 | + assertThat( result, instanceOf( DomesticCustomer.class ) ); |
| 62 | + final DomesticCustomer customer = (DomesticCustomer) result; |
| 63 | + assertThat( customer.getName(), is( "domestic" ) ); |
| 64 | + assertThat( ( customer ).getTaxId(), is( "123" ) ); |
| 65 | + } |
| 66 | + else if ( result.getId() == 2 ) { |
| 67 | + assertThat( result.getId(), is( 2 ) ); |
| 68 | + final ForeignCustomer customer = (ForeignCustomer) result; |
| 69 | + assertThat( customer.getName(), is( "foreign" ) ); |
| 70 | + assertThat( ( customer ).getVat(), is( "987" ) ); |
| 71 | + } |
| 72 | + else { |
| 73 | + assertThat( result.getId(), is( 3 ) ); |
| 74 | + final Customer customer = result; |
| 75 | + assertThat( customer.getName(), is( "customer" ) ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void rootQueryExecutionTest2(SessionFactoryScope scope) { |
| 86 | + scope.inTransaction( |
| 87 | + session -> { |
| 88 | + { |
| 89 | + // [name, taxId, vat] |
| 90 | + final List<Person> results = session.createQuery( |
| 91 | + "select p from Person p", |
| 92 | + Person.class |
| 93 | + ).list(); |
| 94 | + |
| 95 | + assertThat( results.size(), is( 4 ) ); |
| 96 | + |
| 97 | + for ( Person result : results ) { |
| 98 | + if ( result.getId() == 1 ) { |
| 99 | + assertThat( result, instanceOf( DomesticCustomer.class ) ); |
| 100 | + final DomesticCustomer customer = (DomesticCustomer) result; |
| 101 | + assertThat( customer.getName(), is( "domestic" ) ); |
| 102 | + assertThat( ( customer ).getTaxId(), is( "123" ) ); |
| 103 | + } |
| 104 | + else if ( result.getId() == 2 ) { |
| 105 | + assertThat( result.getId(), is( 2 ) ); |
| 106 | + final ForeignCustomer customer = (ForeignCustomer) result; |
| 107 | + assertThat( customer.getName(), is( "foreign" ) ); |
| 108 | + assertThat( ( customer ).getVat(), is( "987" ) ); |
| 109 | + } |
| 110 | + else if ( result.getId() == 3 ) { |
| 111 | + final Customer customer = (Customer) result; |
| 112 | + assertThat( customer.getName(), is( "customer" ) ); |
| 113 | + } |
| 114 | + else { |
| 115 | + assertThat( result.getId(), is( 4 ) ); |
| 116 | + Person person = result; |
| 117 | + assertThat( person.getAge(), is( 23 ) ); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void subclassQueryExecutionTest(SessionFactoryScope scope) { |
| 127 | + scope.inTransaction( |
| 128 | + session -> { |
| 129 | + { |
| 130 | + final DomesticCustomer result = session.createQuery( |
| 131 | + "select c from DomesticCustomer c", |
| 132 | + DomesticCustomer.class |
| 133 | + ).uniqueResult(); |
| 134 | + |
| 135 | + assertThat( result, notNullValue() ); |
| 136 | + assertThat( result.getId(), is( 1 ) ); |
| 137 | + assertThat( result.getName(), is( "domestic" ) ); |
| 138 | + assertThat( result.getTaxId(), is( "123" ) ); |
| 139 | + } |
| 140 | + |
| 141 | + { |
| 142 | + final ForeignCustomer result = session.createQuery( |
| 143 | + "select c from ForeignCustomer c", |
| 144 | + ForeignCustomer.class |
| 145 | + ).uniqueResult(); |
| 146 | + |
| 147 | + assertThat( result, notNullValue() ); |
| 148 | + assertThat( result.getId(), is( 2 ) ); |
| 149 | + assertThat( result.getName(), is( "foreign" ) ); |
| 150 | + assertThat( result.getVat(), is( "987" ) ); |
| 151 | + } |
| 152 | + } |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + @BeforeEach |
| 157 | + public void createTestData(SessionFactoryScope scope) { |
| 158 | + scope.inTransaction( |
| 159 | + session -> { |
| 160 | + Person person = new Person( 4 ); |
| 161 | + person.setAge( 23 ); |
| 162 | + session.persist( person ); |
| 163 | + |
| 164 | + session.persist( new Customer( 3, "customer" ) ); |
| 165 | + session.persist( new DomesticCustomer( 1, "domestic", "123" ) ); |
| 166 | + session.persist( new ForeignCustomer( 2, "foreign", "987" ) ); |
| 167 | + } |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + @AfterEach |
| 172 | + public void cleanupTestData(SessionFactoryScope scope) { |
| 173 | + scope.inTransaction( |
| 174 | + session -> { |
| 175 | + session.createQuery( "from Person", Person.class ).list().forEach( |
| 176 | + cust -> session.delete( cust ) |
| 177 | + ); |
| 178 | + } |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + @Entity(name = "Person") |
| 183 | + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) |
| 184 | + public static class Person { |
| 185 | + private Integer id; |
| 186 | + |
| 187 | + private int age; |
| 188 | + |
| 189 | + Person() { |
| 190 | + } |
| 191 | + |
| 192 | + public Person(Integer id) { |
| 193 | + this.id = id; |
| 194 | + } |
| 195 | + |
| 196 | + @Id |
| 197 | + public Integer getId() { |
| 198 | + return id; |
| 199 | + } |
| 200 | + |
| 201 | + public void setId(Integer id) { |
| 202 | + this.id = id; |
| 203 | + } |
| 204 | + |
| 205 | + public int getAge() { |
| 206 | + return age; |
| 207 | + } |
| 208 | + |
| 209 | + public void setAge(int age) { |
| 210 | + this.age = age; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Entity(name = "Customer") |
| 215 | + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) |
| 216 | + @Table(name = "customer") |
| 217 | + @DiscriminatorColumn(name = "cust_type") |
| 218 | + public static class Customer extends Person { |
| 219 | + |
| 220 | + private String name; |
| 221 | + |
| 222 | + public Customer() { |
| 223 | + } |
| 224 | + |
| 225 | + public Customer(Integer id, String name) { |
| 226 | + super( id ); |
| 227 | + this.name = name; |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + public String getName() { |
| 232 | + return name; |
| 233 | + } |
| 234 | + |
| 235 | + public void setName(String name) { |
| 236 | + this.name = name; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + @Entity(name = "DomesticCustomer") |
| 241 | + @DiscriminatorValue("dc") |
| 242 | + public static class DomesticCustomer extends Customer { |
| 243 | + private String taxId; |
| 244 | + |
| 245 | + public DomesticCustomer() { |
| 246 | + } |
| 247 | + |
| 248 | + public DomesticCustomer(Integer id, String name, String taxId) { |
| 249 | + super( id, name ); |
| 250 | + this.taxId = taxId; |
| 251 | + } |
| 252 | + |
| 253 | + public String getTaxId() { |
| 254 | + return taxId; |
| 255 | + } |
| 256 | + |
| 257 | + public void setTaxId(String taxId) { |
| 258 | + this.taxId = taxId; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @Entity(name = "ForeignCustomer") |
| 263 | + @DiscriminatorValue("fc") |
| 264 | + public static class ForeignCustomer extends Customer { |
| 265 | + private String vat; |
| 266 | + |
| 267 | + public ForeignCustomer() { |
| 268 | + } |
| 269 | + |
| 270 | + public ForeignCustomer(Integer id, String name, String vat) { |
| 271 | + super( id, name ); |
| 272 | + this.vat = vat; |
| 273 | + } |
| 274 | + |
| 275 | + public String getVat() { |
| 276 | + return vat; |
| 277 | + } |
| 278 | + |
| 279 | + public void setVat(String vat) { |
| 280 | + this.vat = vat; |
| 281 | + } |
| 282 | + } |
| 283 | +} |
0 commit comments