From 1582d8acc091e27ef373b2e0e947f581adfe7128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 16 Sep 2021 10:40:55 +0200 Subject: [PATCH] HHH-14827 Test using @AttributeOverride and also an orm.xml file --- ...XmlAndAnnotationAttributeOverrideTest.java | 125 ++++++++++++++++++ .../org/hibernate/test/util/SchemaUtil.java | 18 +++ .../org/hibernate/jpa/test/xml/orm-empty.xml | 13 ++ 3 files changed, 156 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/xml/XmlAndAnnotationAttributeOverrideTest.java create mode 100644 hibernate-core/src/test/resources/org/hibernate/jpa/test/xml/orm-empty.xml diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/xml/XmlAndAnnotationAttributeOverrideTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/xml/XmlAndAnnotationAttributeOverrideTest.java new file mode 100644 index 0000000000..15b243fc2d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/xml/XmlAndAnnotationAttributeOverrideTest.java @@ -0,0 +1,125 @@ +/* + * 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 . + */ +package org.hibernate.jpa.test.xml; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.persistence.AttributeOverride; +import javax.persistence.Column; +import javax.persistence.Embeddable; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.test.util.SchemaUtil; +import org.junit.Test; + +public class XmlAndAnnotationAttributeOverrideTest extends BaseEntityManagerFunctionalTestCase { + @Test + @TestForIssue(jiraKey = "HHH-14827") + public void testDerivedClassAttributeOverriding() { + assertThat( SchemaUtil.getColumnNames( entityManagerFactory(), DerivedEntityType.class ) ) + .contains( "custom_name" ) + .doesNotContain( "name" ); + } + + @Test + public void testEmbeddedAttributeOverriding() { + assertThat( SchemaUtil.getColumnNames( entityManagerFactory(), DerivedEntityType.class ) ) + .contains( "custom_embeddable_name" ) + .doesNotContain( "embeddable_name" ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { MappedSuperclassType.class, DerivedEntityType.class, EmbeddableType.class }; + } + + @Override + public String[] getEjb3DD() { + return new String[] { + // Using an empty orm.xml: the mere presence of an orm.xml used to trigger the bug, + // regardless of its content. + "org/hibernate/jpa/test/xml/orm-empty.xml" + }; + } + + @MappedSuperclass + public static class MappedSuperclassType { + + private String name; + + public MappedSuperclassType() { + } + + public MappedSuperclassType(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "derivedentity") + @AttributeOverride(name = "name", column = @Column(name = "custom_name")) + public static class DerivedEntityType extends MappedSuperclassType { + + @Id + @GeneratedValue + private long id; + + @Embedded + @AttributeOverride(name = "embeddableName", column = @Column(name = "custom_embeddable_name")) + private EmbeddableType embedded; + + public DerivedEntityType() { + } + + public DerivedEntityType(String name) { + super( name ); + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableType getEmbedded() { + return embedded; + } + + public void setEmbedded(EmbeddableType embedded) { + this.embedded = embedded; + } + } + + @Embeddable + public static class EmbeddableType { + private String embeddableName; + + public String getEmbeddableName() { + return embeddableName; + } + + public void setEmbeddableName(String embeddableName) { + this.embeddableName = embeddableName; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/SchemaUtil.java b/hibernate-core/src/test/java/org/hibernate/test/util/SchemaUtil.java index 3cdb27891e..61d8001fda 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/util/SchemaUtil.java +++ b/hibernate-core/src/test/java/org/hibernate/test/util/SchemaUtil.java @@ -6,13 +6,17 @@ */ package org.hibernate.test.util; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import javax.persistence.EntityManagerFactory; import org.hibernate.boot.Metadata; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.mapping.Column; import org.hibernate.mapping.Table; +import org.hibernate.persister.entity.AbstractEntityPersister; /** * Check that the Hibernate metamodel contains some database objects @@ -60,4 +64,18 @@ public abstract class SchemaUtil { return false; } + + public static Set getColumnNames(EntityManagerFactory entityManagerFactory, Class entityType) { + Set result = new HashSet<>(); + AbstractEntityPersister persister = (AbstractEntityPersister) entityManagerFactory + .unwrap( SessionFactoryImplementor.class ) + .getMetamodel().entityPersister( entityType ); + if ( persister == null ) { + return result; + } + for ( String propertyName : persister.getPropertyNames() ) { + Collections.addAll( result, persister.getPropertyColumnNames( propertyName ) ); + } + return result; + } } diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/xml/orm-empty.xml b/hibernate-core/src/test/resources/org/hibernate/jpa/test/xml/orm-empty.xml new file mode 100644 index 0000000000..7e09f0ebc7 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/xml/orm-empty.xml @@ -0,0 +1,13 @@ + + + + + \ No newline at end of file