Skip to content

Commit 151719b

Browse files
committed
HHH-8812 - JPAOverriddenAnnotationReader and @convert annotations
(cherry picked from commit 56e6db3)
1 parent bb26729 commit 151719b

File tree

10 files changed

+628
-226
lines changed

10 files changed

+628
-226
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAOverriddenAnnotationReader.java

Lines changed: 302 additions & 171 deletions
Large diffs are not rendered by default.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.cfg.annotations.reflection;
25+
26+
import org.hibernate.annotations.common.reflection.Filter;
27+
28+
/**
29+
* @author Emmanuel Bernard
30+
* @author Steve Ebersole
31+
*/
32+
public class PersistentAttributeFilter implements Filter {
33+
/**
34+
* Singleton access
35+
*/
36+
public static final PersistentAttributeFilter INSTANCE = new PersistentAttributeFilter();
37+
38+
@Override
39+
public boolean returnStatic() {
40+
return false;
41+
}
42+
43+
@Override
44+
public boolean returnTransient() {
45+
return false;
46+
}
47+
}

hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/XMLContext.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
* 51 Franklin Street, Fifth Floor
2222
* Boston, MA 02110-1301 USA
2323
*/
24-
25-
// $Id$
26-
2724
package org.hibernate.cfg.annotations.reflection;
2825

2926
import java.io.Serializable;
@@ -37,21 +34,23 @@
3734
import org.hibernate.AnnotationException;
3835
import org.hibernate.cfg.AttributeConverterDefinition;
3936
import org.hibernate.cfg.Configuration;
37+
import org.hibernate.internal.CoreLogging;
4038
import org.hibernate.internal.CoreMessageLogger;
4139
import org.hibernate.internal.util.ReflectHelper;
4240
import org.hibernate.internal.util.StringHelper;
4341

44-
import org.jboss.logging.Logger;
45-
4642
import org.dom4j.Document;
4743
import org.dom4j.Element;
4844

4945
/**
46+
* A helper for consuming orm.xml mappings.
47+
*
5048
* @author Emmanuel Bernard
5149
* @author Brett Meyer
5250
*/
5351
public class XMLContext implements Serializable {
54-
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, XMLContext.class.getName());
52+
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( XMLContext.class );
53+
5554
private Default globalDefaults;
5655
private Map<String, Element> classOverriding = new HashMap<String, Element>();
5756
private Map<String, Default> defaultsOverriding = new HashMap<String, Default>();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
25+
/**
26+
* Defines the capability to merge mapping information from both annotations and orm.xml sources into a unified set of
27+
* metadata in the Hibernate commons-annotations model.
28+
*/
29+
package org.hibernate.cfg.annotations.reflection;

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitDateConvertersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class ExplicitDateConvertersTest {
5858

5959
// NOTE : initially unable to reproduce the reported problem
6060

61+
static int callsToConverter = 0;
62+
6163
public static class LongToDateConverter implements AttributeConverter<Date,Long> {
6264
@Override
6365
public Long convertToDatabaseColumn(Date attribute) {
@@ -72,8 +74,6 @@ public Date convertToEntityAttribute(Long dbData) {
7274
}
7375
}
7476

75-
static int callsToConverter = 0;
76-
7777
@Entity( name = "Entity1" )
7878
public static class Entity1 {
7979
@Id

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleXmlOverriddenTest.java

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,44 @@
2323
*/
2424
package org.hibernate.jpa.test.convert;
2525

26-
import javax.persistence.Convert;
27-
import javax.persistence.Entity;
28-
import javax.persistence.EntityManagerFactory;
29-
import javax.persistence.Id;
30-
import javax.persistence.MappedSuperclass;
31-
3226
import java.util.Arrays;
3327
import java.util.Collections;
3428
import java.util.HashMap;
3529
import java.util.List;
3630
import java.util.Map;
31+
import javax.persistence.Convert;
32+
import javax.persistence.Entity;
33+
import javax.persistence.EntityManagerFactory;
34+
import javax.persistence.Id;
3735

38-
import org.hibernate.cfg.AvailableSettings;
3936
import org.hibernate.engine.spi.SessionFactoryImplementor;
4037
import org.hibernate.jpa.boot.spi.Bootstrap;
4138
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
4239
import org.hibernate.persister.entity.EntityPersister;
4340
import org.hibernate.type.StringType;
4441
import org.hibernate.type.Type;
42+
import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter;
4543

46-
import org.junit.Test;
47-
48-
import org.hibernate.testing.FailureExpected;
4944
import org.hibernate.testing.junit4.BaseUnitTestCase;
45+
import org.junit.Test;
5046

5147
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
52-
import static org.junit.Assert.fail;
5348

5449
/**
5550
* Test simple application of Convert annotation via XML.
5651
*
5752
* @author Steve Ebersole
5853
*/
5954
public class SimpleXmlOverriddenTest extends BaseUnitTestCase {
55+
/**
56+
* A baseline test, with an explicit @Convert annotation that should be in effect
57+
*/
6058
@Test
6159
public void baseline() {
6260
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
6361
@Override
6462
public List<String> getManagedClassNames() {
65-
return Arrays.asList( Super.class.getName(), Sub.class.getName() );
63+
return Arrays.asList( TheEntity.class.getName() );
6664
}
6765

6866
// No mapping file should mean that the converter is applied
@@ -74,30 +72,26 @@ public List<String> getManagedClassNames() {
7472

7573
final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class );
7674
try {
77-
final EntityPersister ep = sfi.getEntityPersister( Sub.class.getName() );
75+
final EntityPersister ep = sfi.getEntityPersister( TheEntity.class.getName() );
7876

7977
Type type = ep.getPropertyType( "it" );
80-
try {
81-
assertTyping( StringType.class, type );
82-
fail( "Expected AttributeConverter to be applied" );
83-
}
84-
catch (AssertionError expected) {
85-
}
78+
AttributeConverterTypeAdapter adapter = assertTyping( AttributeConverterTypeAdapter.class, type );
79+
assertTyping( SillyStringConverter.class, adapter.getAttributeConverter() );
8680
}
8781
finally {
8882
emf.close();
8983
}
9084
}
9185

9286
/**
93-
* Test outcome of applying overrides via orm.xml, specifically at the entity level
87+
* Test outcome of applying overrides via orm.xml, specifically at the attribute level
9488
*/
9589
@Test
96-
public void testDefinitionAtEntityLevel() {
90+
public void testDefinitionAtAttributeLevel() {
9791
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
9892
@Override
9993
public List<String> getManagedClassNames() {
100-
return Arrays.asList( Super.class.getName(), Sub.class.getName() );
94+
return Arrays.asList( TheEntity.class.getName() );
10195
}
10296

10397
@Override
@@ -107,13 +101,11 @@ public List<String> getMappingFileNames() {
107101
};
108102

109103
final Map settings = new HashMap();
110-
// settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
111-
112104
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build();
113105

114106
final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class );
115107
try {
116-
final EntityPersister ep = sfi.getEntityPersister( Sub.class.getName() );
108+
final EntityPersister ep = sfi.getEntityPersister( TheEntity.class.getName() );
117109

118110
Type type = ep.getPropertyType( "it" );
119111
assertTyping( StringType.class, type );
@@ -127,11 +119,11 @@ public List<String> getMappingFileNames() {
127119
* Test outcome of applying overrides via orm.xml, specifically at the entity level
128120
*/
129121
@Test
130-
public void testDefinitionAtAttributeLevel() {
122+
public void testDefinitionAtEntityLevel() {
131123
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
132124
@Override
133125
public List<String> getManagedClassNames() {
134-
return Arrays.asList( Super.class.getName(), Sub.class.getName() );
126+
return Arrays.asList( TheEntity2.class.getName() );
135127
}
136128

137129
@Override
@@ -141,13 +133,11 @@ public List<String> getMappingFileNames() {
141133
};
142134

143135
final Map settings = new HashMap();
144-
// settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
145-
146136
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build();
147137

148138
final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class );
149139
try {
150-
final EntityPersister ep = sfi.getEntityPersister( Sub.class.getName() );
140+
final EntityPersister ep = sfi.getEntityPersister( TheEntity2.class.getName() );
151141

152142
Type type = ep.getPropertyType( "it" );
153143
assertTyping( StringType.class, type );
@@ -157,18 +147,19 @@ public List<String> getMappingFileNames() {
157147
}
158148
}
159149

160-
@MappedSuperclass
161-
public static class Super {
150+
@Entity(name="TheEntity")
151+
public static class TheEntity {
162152
@Id
163153
public Integer id;
164154
@Convert(converter = SillyStringConverter.class)
165155
public String it;
166156
}
167157

168-
@Entity(name = "Sub")
169-
// the xml disabled conversion on the Sub#it attribute
170-
// Essentially the same test as org.hibernate.jpa.test.convert.SimpleOverriddenConverterTest, but through XML
171-
//@Convert( attributeName = "it", disableConversion = true )
172-
public static class Sub extends Super {
158+
@Entity(name="TheEntity2")
159+
@Convert( attributeName = "it", converter = SillyStringConverter.class )
160+
public static class TheEntity2 {
161+
@Id
162+
public Integer id;
163+
public String it;
173164
}
174165
}

0 commit comments

Comments
 (0)