diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/associationOverride/EmbeddedOverrideTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/associationOverride/EmbeddedOverrideTests.java new file mode 100644 index 0000000000..67ba347034 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/associationOverride/EmbeddedOverrideTests.java @@ -0,0 +1,150 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.bootstrap.binding.annotations.associationOverride; + +import javax.persistence.AssociationOverride; +import javax.persistence.AttributeOverride; +import javax.persistence.Column; +import javax.persistence.Embeddable; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.hibernate.mapping.BasicValue; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; +import org.hibernate.mapping.Selectable; +import org.hibernate.mapping.ToOne; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.DomainModelScope; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.test.util.SchemaUtil; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Steve Ebersole + */ +@DomainModel( + annotatedClasses = { + EmbeddedOverrideTests.EmbeddedOverrideContact.class, + EmbeddedOverrideTests.EmbeddedOverrideAddress.class, + EmbeddedOverrideTests.EmbeddedOverrideState.class + } +) +@SessionFactory +public class EmbeddedOverrideTests { + + @Test + public void testMapping(DomainModelScope scope) { + final PersistentClass contactBinding = scope.getDomainModel().getEntityBinding( EmbeddedOverrideContact.class.getName() ); + + SchemaUtil.isColumnPresent( "embedded_override_contact", "home_street_addr", scope.getDomainModel() ); + SchemaUtil.isColumnPresent( "embedded_override_contact", "home_city", scope.getDomainModel() ); + SchemaUtil.isColumnPresent( "embedded_override_contact", "home_state_id", scope.getDomainModel() ); + + SchemaUtil.isColumnPresent( "embedded_override_contact", "work_street_addr", scope.getDomainModel() ); + SchemaUtil.isColumnPresent( "embedded_override_contact", "work_city", scope.getDomainModel() ); + SchemaUtil.isColumnPresent( "embedded_override_contact", "work_state_id", scope.getDomainModel() ); + + final Property homeAddressProperty = contactBinding.getProperty( "homeAddress" ); + final Component homeAddressMapping = (Component) homeAddressProperty.getValue(); + final Property homeAddressStateProperty = homeAddressMapping.getProperty( "state" ); + final ToOne homeAddressStateMapping = (ToOne) homeAddressStateProperty.getValue(); + assertThat( homeAddressStateMapping.getColumnSpan(), is( 1 ) ); + final org.hibernate.mapping.Column homeAddressStateJoinColumn = (org.hibernate.mapping.Column) homeAddressStateMapping.getColumnIterator().next(); + assertThat( homeAddressStateJoinColumn.getName(), is ( "home_state_id" ) ); + + final Property workAddressProperty = contactBinding.getProperty( "workAddress" ); + final Component workAddressMapping = (Component) workAddressProperty.getValue(); + final Property workAddressStateProperty = workAddressMapping.getProperty( "state" ); + final ToOne workAddressStateMapping = (ToOne) workAddressStateProperty.getValue(); + assertThat( workAddressStateMapping.getColumnSpan(), is( 1 ) ); + final org.hibernate.mapping.Column workAddressStateJoinColumn = (org.hibernate.mapping.Column) workAddressStateMapping.getColumnIterator().next(); + assertThat( workAddressStateJoinColumn.getName(), is ( "work_state_id" ) ); + } + + @Entity( name = "EmbeddedOverrideContact" ) + @Table( name = "embedded_override_contact" ) + public static class EmbeddedOverrideContact { + @Id + private Integer id; + private String name; + + @Embedded + @AttributeOverride( name = "streetAddress", column = @Column( name = "home_street_addr" ) ) + @AttributeOverride( name = "city", column = @Column( name = "home_city" ) ) + @AssociationOverride( name = "state", joinColumns = @JoinColumn( name = "home_state_id" ) ) + private EmbeddedOverrideAddress homeAddress; + + @Embedded + @AttributeOverride( name = "streetAddress", column = @Column( name = "work_street_addr" ) ) + @AttributeOverride( name = "city", column = @Column( name = "work_city" ) ) + @AssociationOverride( name = "state", joinColumns = @JoinColumn( name = "work_state_id" ) ) + private EmbeddedOverrideAddress workAddress; + } + + @Embeddable + public static class EmbeddedOverrideAddress { + private String streetAddress; + private String city; + @ManyToOne + @JoinColumn + private EmbeddedOverrideState state; + + } + + @Entity( name= "EmbeddedOverrideState" ) + @Table( name = "embedded_override_state" ) + public static class EmbeddedOverrideState { + @Id + private Integer id; + private String code; + private String name; + + public EmbeddedOverrideState() { + } + + public EmbeddedOverrideState(Integer id, String code, String name) { + this.id = id; + this.code = code; + this.name = name; + } + + protected Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/associationOverride/MappedSuperclassOverrideTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/associationOverride/MappedSuperclassOverrideTests.java new file mode 100644 index 0000000000..499db41f35 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/associationOverride/MappedSuperclassOverrideTests.java @@ -0,0 +1,285 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.bootstrap.binding.annotations.associationOverride; + +import javax.persistence.AssociationOverride; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MappedSuperclass; +import javax.persistence.Table; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.DomainModelScope; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.test.util.SchemaUtil; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Andrea Boriero + */ +@DomainModel( + annotatedClasses = { + MappedSuperclassOverrideTests.Customer.class, + MappedSuperclassOverrideTests.DomesticCustomer.class, + MappedSuperclassOverrideTests.ForeignCustomer.class, + MappedSuperclassOverrideTests.Address.class + } +) +@ServiceRegistry +@SessionFactory +public class MappedSuperclassOverrideTests { + + @Test + public void testMapping(DomainModelScope scope) { + assertTrue( SchemaUtil.isColumnPresent( "DOMESTIC_CUSTOMER", "dc_home_addr_id", scope.getDomainModel() ) ); + assertTrue( SchemaUtil.isColumnPresent( "FOREIGN_CUSTOMER", "fc_home_addr_id", scope.getDomainModel() ) ); + } + + @Test + public void testQuery(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + DomesticCustomer domesticCustomer = session.createQuery( + "from DomesticCustomer c", + DomesticCustomer.class + ).getSingleResult(); + assertThat( domesticCustomer.getName(), is( "domestic" ) ); + assertThat( domesticCustomer.getHomeAddress().getCity(), is( "London" ) ); + assertThat( domesticCustomer.getWorkAddress(), nullValue() ); + + ForeignCustomer foreignCustomer = session.createQuery( + "from ForeignCustomer c", + ForeignCustomer.class + ).getSingleResult(); + assertThat( foreignCustomer.getName(), is( "foreign" ) ); + assertThat( foreignCustomer.getHomeAddress().getCity(), is( "London" ) ); + assertThat( foreignCustomer.getWorkAddress(), nullValue() ); + } + ); + } + + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + final Address address = new Address( 1, "Kennington road", "London" ); + + final DomesticCustomer domestic = new DomesticCustomer( + 1, + "domestic", + address, + null, + "123" + ); + + final ForeignCustomer foreign = new ForeignCustomer( + 1, + "foreign", + address, + null, + "123" + ); + + session.persist( domestic ); + session.persist( foreign ); + } + ); + } + + @AfterEach + public void cleanupTestData(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete DomesticCustomer" ).executeUpdate(); + session.createQuery( "delete ForeignCustomer" ).executeUpdate(); + session.createQuery( "delete Address" ).executeUpdate(); + } + ); + } + + @Entity( name = "Address" ) + public static class Address { + @Id + private Integer id; + private String street; + private String city; + + public Address() { + } + + public Address(Integer id, String street, String city) { + this.id = id; + this.street = street; + this.city = city; + } + + + @Column(name = "STREET") + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + } + + + @MappedSuperclass + public static abstract class Customer { + @Id + private Integer id; + private String name; + @ManyToOne( cascade = CascadeType.ALL ) + @JoinColumn + private Address homeAddress; + @ManyToOne( cascade = CascadeType.ALL ) + @JoinColumn + private Address workAddress; + + public Customer() { + } + + public Customer(Integer id, String name) { + this.id = id; + this.name = name; + } + + public Customer( + Integer id, + String name, + Address homeAddress, + Address workAddress) { + this.id = id; + this.name = name; + this.homeAddress = homeAddress; + this.workAddress = workAddress; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getHomeAddress() { + return homeAddress; + } + + public void setHomeAddress(Address homeAddress) { + this.homeAddress = homeAddress; + } + + public Address getWorkAddress() { + return workAddress; + } + + public void setWorkAddress(Address workAddress) { + this.workAddress = workAddress; + } + + } + + @Entity(name = "DomesticCustomer") + @Table(name = "DOMESTIC_CUSTOMER") + @AssociationOverride( name = "homeAddress", joinColumns = @JoinColumn( name = "dc_home_addr_id") ) + @AssociationOverride( name = "workAddress", joinColumns = @JoinColumn( name = "dc_work_addr_id") ) + public static class DomesticCustomer extends Customer { + private String taxId; + + public DomesticCustomer() { + } + + public DomesticCustomer(Integer id, String name, String taxId) { + super( id, name ); + this.taxId = taxId; + } + + public DomesticCustomer( + Integer id, + String name, + Address homeAddress, + Address workAddress, + String taxId) { + super( id, name, homeAddress, workAddress ); + this.taxId = taxId; + } + + public String getTaxId() { + return taxId; + } + + public void setTaxId(String taxId) { + this.taxId = taxId; + } + } + + @Entity(name = "ForeignCustomer") + @Table(name = "FOREIGN_CUSTOMER") + @AssociationOverride( name = "homeAddress", joinColumns = @JoinColumn( name = "fc_home_addr_id") ) + @AssociationOverride( name = "workAddress", joinColumns = @JoinColumn( name = "fc_work_addr_id") ) + public static class ForeignCustomer extends Customer { + private String vat; + + public ForeignCustomer() { + } + + public ForeignCustomer(Integer id, String name, String vat) { + super( id, name ); + this.vat = vat; + } + + public ForeignCustomer( + Integer id, + String name, + Address homeAddress, + Address workAddress, + String vat) { + super( id, name, homeAddress, workAddress ); + this.vat = vat; + } + + public String getVat() { + return vat; + } + + public void setVat(String vat) { + this.vat = vat; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/inheritance/MappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/MappedSuperclassOverrideTests.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/inheritance/MappedSuperclassTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/MappedSuperclassOverrideTests.java index 8eebb40fb2..f13dc61d67 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/inheritance/MappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/MappedSuperclassOverrideTests.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html */ -package org.hibernate.orm.test.bootstrap.binding.annotations.attributeOverrides.inheritance; +package org.hibernate.orm.test.bootstrap.binding.annotations.attributeOverrides; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; @@ -37,14 +37,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ @DomainModel( annotatedClasses = { - MappedSuperclassTest.Customer.class, - MappedSuperclassTest.DomesticCustomer.class, - MappedSuperclassTest.ForeignCustomer.class + MappedSuperclassOverrideTests.Customer.class, + MappedSuperclassOverrideTests.DomesticCustomer.class, + MappedSuperclassOverrideTests.ForeignCustomer.class } ) @ServiceRegistry @SessionFactory -public class MappedSuperclassTest { +public class MappedSuperclassOverrideTests { @Test public void testSchema(SessionFactoryScope scope) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/PluralAttributeOverridesTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/PluralEmbeddedOverrideTests.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/PluralAttributeOverridesTests.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/PluralEmbeddedOverrideTests.java index 1ad4fb26be..93c0a4bec2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/PluralAttributeOverridesTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/PluralEmbeddedOverrideTests.java @@ -53,13 +53,13 @@ import static org.junit.Assert.assertTrue; */ @DomainModel( annotatedClasses = { - PluralAttributeOverridesTests.TypeValue.class, - PluralAttributeOverridesTests.AggregatedTypeValue.class + PluralEmbeddedOverrideTests.TypeValue.class, + PluralEmbeddedOverrideTests.AggregatedTypeValue.class } ) @ServiceRegistry @SessionFactory -public class PluralAttributeOverridesTests { +public class PluralEmbeddedOverrideTests { @SuppressWarnings("rawtypes") @Test diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/inheritance/TablePerClassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/TablePerClassOverrideTests.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/inheritance/TablePerClassTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/TablePerClassOverrideTests.java index 55bdfdf649..38f9ee7267 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/inheritance/TablePerClassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/annotations/attributeOverrides/TablePerClassOverrideTests.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html */ -package org.hibernate.orm.test.bootstrap.binding.annotations.attributeOverrides.inheritance; +package org.hibernate.orm.test.bootstrap.binding.annotations.attributeOverrides; import java.util.List; import javax.persistence.AttributeOverride; @@ -40,14 +40,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ @DomainModel( annotatedClasses = { - TablePerClassTest.Customer.class, - TablePerClassTest.DomesticCustomer.class, - TablePerClassTest.ForeignCustomer.class + TablePerClassOverrideTests.Customer.class, + TablePerClassOverrideTests.DomesticCustomer.class, + TablePerClassOverrideTests.ForeignCustomer.class } ) @ServiceRegistry @SessionFactory -public class TablePerClassTest { +public class TablePerClassOverrideTests { @Test @FailureExpected(reason = "@AttributeOverrides not applied for Table per class")