|
| 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.sql.Statement; |
| 10 | +import java.util.List; |
| 11 | +import javax.persistence.Entity; |
| 12 | +import javax.persistence.Id; |
| 13 | +import javax.persistence.Inheritance; |
| 14 | +import javax.persistence.InheritanceType; |
| 15 | +import javax.persistence.Table; |
| 16 | + |
| 17 | +import org.hibernate.persister.entity.EntityPersister; |
| 18 | +import org.hibernate.persister.entity.JoinedSubclassEntityPersister; |
| 19 | + |
| 20 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 21 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 22 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 23 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 24 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Tag; |
| 28 | +import org.junit.jupiter.api.Tags; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 32 | +import static org.hamcrest.CoreMatchers.is; |
| 33 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 34 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Andrea Boriero |
| 38 | + */ |
| 39 | +@DomainModel( |
| 40 | + annotatedClasses = { |
| 41 | + JoinedInheritanceTest.Customer.class, |
| 42 | + JoinedInheritanceTest.DomesticCustomer.class, |
| 43 | + JoinedInheritanceTest.ForeignCustomer.class |
| 44 | + } |
| 45 | +) |
| 46 | +@ServiceRegistry |
| 47 | +@SessionFactory |
| 48 | +@Tags({ |
| 49 | + @Tag("RunnableIdeTest"), |
| 50 | +}) |
| 51 | +public class JoinedInheritanceTest { |
| 52 | + |
| 53 | + @Test |
| 54 | + public void basicTest(SessionFactoryScope scope) { |
| 55 | + final EntityPersister customerDescriptor = scope.getSessionFactory() |
| 56 | + .getMetamodel() |
| 57 | + .findEntityDescriptor( Customer.class ); |
| 58 | + final EntityPersister domesticCustomerDescriptor = scope.getSessionFactory() |
| 59 | + .getMetamodel() |
| 60 | + .findEntityDescriptor( DomesticCustomer.class ); |
| 61 | + final EntityPersister foreignCustomerDescriptor = scope.getSessionFactory() |
| 62 | + .getMetamodel() |
| 63 | + .findEntityDescriptor( ForeignCustomer.class ); |
| 64 | + |
| 65 | + assert customerDescriptor instanceof JoinedSubclassEntityPersister; |
| 66 | + |
| 67 | + assert customerDescriptor.isTypeOrSuperType( customerDescriptor ); |
| 68 | + assert !customerDescriptor.isTypeOrSuperType( domesticCustomerDescriptor ); |
| 69 | + assert !customerDescriptor.isTypeOrSuperType( foreignCustomerDescriptor ); |
| 70 | + |
| 71 | + assert domesticCustomerDescriptor instanceof JoinedSubclassEntityPersister; |
| 72 | + |
| 73 | + assert domesticCustomerDescriptor.isTypeOrSuperType( customerDescriptor ); |
| 74 | + assert domesticCustomerDescriptor.isTypeOrSuperType( domesticCustomerDescriptor ); |
| 75 | + assert !domesticCustomerDescriptor.isTypeOrSuperType( foreignCustomerDescriptor ); |
| 76 | + |
| 77 | + assert foreignCustomerDescriptor instanceof JoinedSubclassEntityPersister; |
| 78 | + |
| 79 | + assert foreignCustomerDescriptor.isTypeOrSuperType( customerDescriptor ); |
| 80 | + assert !foreignCustomerDescriptor.isTypeOrSuperType( domesticCustomerDescriptor ); |
| 81 | + assert foreignCustomerDescriptor.isTypeOrSuperType( foreignCustomerDescriptor ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @FailureExpected |
| 86 | + public void rootQueryExecutionTest(SessionFactoryScope scope) { |
| 87 | + scope.inTransaction( |
| 88 | + session -> { |
| 89 | + { |
| 90 | + // [name, taxId, vat] |
| 91 | + final List<Customer> results = session.createQuery( |
| 92 | + "select c from Customer c", |
| 93 | + Customer.class |
| 94 | + ).list(); |
| 95 | + |
| 96 | + assertThat( results.size(), is( 2 ) ); |
| 97 | + |
| 98 | + for ( Customer result : results ) { |
| 99 | + if ( result.getId() == 1 ) { |
| 100 | + assertThat( result, instanceOf( DomesticCustomer.class ) ); |
| 101 | + final DomesticCustomer customer = (DomesticCustomer) result; |
| 102 | + assertThat( customer.getName(), is( "domestic" ) ); |
| 103 | + assertThat( ( customer ).getTaxId(), is( "123" ) ); |
| 104 | + } |
| 105 | + else { |
| 106 | + assertThat( result.getId(), is( 2 ) ); |
| 107 | + final ForeignCustomer customer = (ForeignCustomer) result; |
| 108 | + assertThat( customer.getName(), is( "foreign" ) ); |
| 109 | + assertThat( ( customer ).getVat(), is( "987" ) ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void subclassQueryExecutionTest(SessionFactoryScope scope) { |
| 120 | + scope.inTransaction( |
| 121 | + session -> { |
| 122 | + { |
| 123 | + final DomesticCustomer result = session.createQuery( |
| 124 | + "select c from DomesticCustomer c", |
| 125 | + DomesticCustomer.class |
| 126 | + ).uniqueResult(); |
| 127 | + |
| 128 | + assertThat( result, notNullValue() ); |
| 129 | + assertThat( result.getId(), is( 1 ) ); |
| 130 | + assertThat( result.getName(), is( "domestic" ) ); |
| 131 | + assertThat( result.getTaxId(), is( "123" ) ); |
| 132 | + } |
| 133 | + |
| 134 | + { |
| 135 | + final ForeignCustomer result = session.createQuery( |
| 136 | + "select c from ForeignCustomer c", |
| 137 | + ForeignCustomer.class |
| 138 | + ).uniqueResult(); |
| 139 | + |
| 140 | + assertThat( result, notNullValue() ); |
| 141 | + assertThat( result.getId(), is( 2 ) ); |
| 142 | + assertThat( result.getName(), is( "foreign" ) ); |
| 143 | + assertThat( result.getVat(), is( "987" ) ); |
| 144 | + } |
| 145 | + } |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + @BeforeEach |
| 150 | + public void createTestData(SessionFactoryScope scope) { |
| 151 | + scope.inTransaction( |
| 152 | + session -> { |
| 153 | + session.persist( new DomesticCustomer( 1, "domestic", "123" ) ); |
| 154 | + session.persist( new ForeignCustomer( 2, "foreign", "987" ) ); |
| 155 | + } |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + @AfterEach |
| 160 | + public void cleanupTestData(SessionFactoryScope scope) { |
| 161 | + scope.inTransaction( |
| 162 | + session -> { |
| 163 | + session.doWork( |
| 164 | + work -> { |
| 165 | + Statement statement = work.createStatement(); |
| 166 | + try { |
| 167 | + statement.execute( "delete from DomesticCustomer" ); |
| 168 | + statement.execute( "delete from ForeignCustomer" ); |
| 169 | + statement.execute( "delete from Customer" ); |
| 170 | + } |
| 171 | + finally { |
| 172 | + statement.close(); |
| 173 | + } |
| 174 | + } |
| 175 | + ); |
| 176 | +// session.createQuery( "from DomesticCustomer", DomesticCustomer.class ).list().forEach( |
| 177 | +// cust -> session.delete( cust ) |
| 178 | +// ); |
| 179 | +// session.createQuery( "from ForeignCustomer", ForeignCustomer.class ).list().forEach( |
| 180 | +// cust -> session.delete( cust ) |
| 181 | +// ); |
| 182 | + } |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + @Entity(name = "Customer") |
| 187 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 188 | + @Table(name = "Customer") |
| 189 | + public static abstract class Customer { |
| 190 | + private Integer id; |
| 191 | + private String name; |
| 192 | + |
| 193 | + public Customer() { |
| 194 | + } |
| 195 | + |
| 196 | + public Customer(Integer id, String name) { |
| 197 | + this.id = id; |
| 198 | + this.name = name; |
| 199 | + } |
| 200 | + |
| 201 | + @Id |
| 202 | + public Integer getId() { |
| 203 | + return id; |
| 204 | + } |
| 205 | + |
| 206 | + public void setId(Integer id) { |
| 207 | + this.id = id; |
| 208 | + } |
| 209 | + |
| 210 | + public String getName() { |
| 211 | + return name; |
| 212 | + } |
| 213 | + |
| 214 | + public void setName(String name) { |
| 215 | + this.name = name; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + @Entity(name = "DomesticCustomer") |
| 220 | + @Table(name = "DomesticCustomer") |
| 221 | + public static class DomesticCustomer extends Customer { |
| 222 | + private String taxId; |
| 223 | + |
| 224 | + public DomesticCustomer() { |
| 225 | + } |
| 226 | + |
| 227 | + public DomesticCustomer(Integer id, String name, String taxId) { |
| 228 | + super( id, name ); |
| 229 | + this.taxId = taxId; |
| 230 | + } |
| 231 | + |
| 232 | + public String getTaxId() { |
| 233 | + return taxId; |
| 234 | + } |
| 235 | + |
| 236 | + public void setTaxId(String taxId) { |
| 237 | + this.taxId = taxId; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + @Entity(name = "ForeignCustomer") |
| 242 | + @Table(name = "ForeignCustomer") |
| 243 | + public static class ForeignCustomer extends Customer { |
| 244 | + private String vat; |
| 245 | + |
| 246 | + public ForeignCustomer() { |
| 247 | + } |
| 248 | + |
| 249 | + public ForeignCustomer(Integer id, String name, String vat) { |
| 250 | + super( id, name ); |
| 251 | + this.vat = vat; |
| 252 | + } |
| 253 | + |
| 254 | + public String getVat() { |
| 255 | + return vat; |
| 256 | + } |
| 257 | + |
| 258 | + public void setVat(String vat) { |
| 259 | + this.vat = vat; |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments