HHH-14827 Test using @AttributeOverride and also an orm.xml file

This commit is contained in:
Yoann Rodière 2021-09-16 10:40:55 +02:00
parent f2db7302c9
commit 1582d8acc0
3 changed files with 156 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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<String> getColumnNames(EntityManagerFactory entityManagerFactory, Class<?> entityType) {
Set<String> 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;
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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>.
-->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
</entity-mappings>