Skip to content

Commit 170821c

Browse files
committed
HHH-14827 Test using @AttributeOverride and also an orm.xml file
1 parent f6a03cf commit 170821c

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.jpa.test.xml;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
import javax.persistence.AttributeOverride;
12+
import javax.persistence.Column;
13+
import javax.persistence.Embeddable;
14+
import javax.persistence.Embedded;
15+
import javax.persistence.Entity;
16+
import javax.persistence.GeneratedValue;
17+
import javax.persistence.Id;
18+
import javax.persistence.MappedSuperclass;
19+
20+
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
21+
22+
import org.hibernate.testing.TestForIssue;
23+
import org.hibernate.test.util.SchemaUtil;
24+
import org.junit.Test;
25+
26+
public class XmlAndAnnotationAttributeOverrideTest extends BaseEntityManagerFunctionalTestCase {
27+
@Test
28+
@TestForIssue(jiraKey = "HHH-14827")
29+
public void testDerivedClassAttributeOverriding() {
30+
assertThat( SchemaUtil.getColumnNames( entityManagerFactory(), DerivedEntityType.class ) )
31+
.contains( "custom_name" )
32+
.doesNotContain( "name" );
33+
}
34+
35+
@Test
36+
public void testEmbeddedAttributeOverriding() {
37+
assertThat( SchemaUtil.getColumnNames( entityManagerFactory(), DerivedEntityType.class ) )
38+
.contains( "custom_embeddable_name" )
39+
.doesNotContain( "embeddable_name" );
40+
}
41+
42+
@Override
43+
protected Class<?>[] getAnnotatedClasses() {
44+
return new Class[] { MappedSuperclassType.class, DerivedEntityType.class, EmbeddableType.class };
45+
}
46+
47+
@Override
48+
public String[] getEjb3DD() {
49+
return new String[] {
50+
// Using an empty orm.xml: the mere presence of an orm.xml used to trigger the bug,
51+
// regardless of its content.
52+
"org/hibernate/jpa/test/xml/orm-empty.xml"
53+
};
54+
}
55+
56+
@MappedSuperclass
57+
public static class MappedSuperclassType {
58+
59+
private String name;
60+
61+
public MappedSuperclassType() {
62+
}
63+
64+
public MappedSuperclassType(String name) {
65+
this.name = name;
66+
}
67+
68+
public String getName() {
69+
return name;
70+
}
71+
72+
public void setName(String name) {
73+
this.name = name;
74+
}
75+
}
76+
77+
@Entity(name = "derivedentity")
78+
@AttributeOverride(name = "name", column = @Column(name = "custom_name"))
79+
public static class DerivedEntityType extends MappedSuperclassType {
80+
81+
@Id
82+
@GeneratedValue
83+
private long id;
84+
85+
@Embedded
86+
@AttributeOverride(name = "embeddableName", column = @Column(name = "custom_embeddable_name"))
87+
private EmbeddableType embedded;
88+
89+
public DerivedEntityType() {
90+
}
91+
92+
public DerivedEntityType(String name) {
93+
super( name );
94+
}
95+
96+
public long getId() {
97+
return id;
98+
}
99+
100+
public void setId(long id) {
101+
this.id = id;
102+
}
103+
104+
public EmbeddableType getEmbedded() {
105+
return embedded;
106+
}
107+
108+
public void setEmbedded(EmbeddableType embedded) {
109+
this.embedded = embedded;
110+
}
111+
}
112+
113+
@Embeddable
114+
public static class EmbeddableType {
115+
private String embeddableName;
116+
117+
public String getEmbeddableName() {
118+
return embeddableName;
119+
}
120+
121+
public void setEmbeddableName(String embeddableName) {
122+
this.embeddableName = embeddableName;
123+
}
124+
}
125+
}

hibernate-core/src/test/java/org/hibernate/test/util/SchemaUtil.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
*/
77
package org.hibernate.test.util;
88

9+
import java.util.Collections;
910
import java.util.HashSet;
1011
import java.util.Iterator;
1112
import java.util.Set;
13+
import javax.persistence.EntityManagerFactory;
1214

1315
import org.hibernate.boot.Metadata;
16+
import org.hibernate.engine.spi.SessionFactoryImplementor;
1417
import org.hibernate.mapping.Column;
1518
import org.hibernate.mapping.Table;
19+
import org.hibernate.persister.entity.AbstractEntityPersister;
1620

1721
/**
1822
* Check that the Hibernate metamodel contains some database objects
@@ -60,4 +64,18 @@ public static boolean isTablePresent(String tableName, Metadata metadata) {
6064

6165
return false;
6266
}
67+
68+
public static Set<String> getColumnNames(EntityManagerFactory entityManagerFactory, Class<?> entityType) {
69+
Set<String> result = new HashSet<>();
70+
AbstractEntityPersister persister = (AbstractEntityPersister) entityManagerFactory
71+
.unwrap( SessionFactoryImplementor.class )
72+
.getMetamodel().entityPersister( entityType );
73+
if ( persister == null ) {
74+
return result;
75+
}
76+
for ( String propertyName : persister.getPropertyNames() ) {
77+
Collections.addAll( result, persister.getPropertyColumnNames( propertyName ) );
78+
}
79+
return result;
80+
}
6381
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
~ Hibernate, Relational Persistence for Idiomatic Java
5+
~
6+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
7+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
8+
-->
9+
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
10+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
12+
version="2.0">
13+
</entity-mappings>

0 commit comments

Comments
 (0)