HHH-8111 - AttributeConverter doesn't override built-in type mappings

This commit is contained in:
Steve Ebersole 2013-08-19 16:06:51 -05:00
parent ad5c0f1f82
commit bf168ca24e
5 changed files with 209 additions and 32 deletions

View File

@ -329,6 +329,9 @@ public class SimpleValueBinder {
private AttributeConverterDefinition locateAutoApplyAttributeConverter(XProperty property) {
final Class propertyType = mappings.getReflectionManager().toClass( property.getType() );
for ( AttributeConverterDefinition attributeConverterDefinition : mappings.getAttributeConverters() ) {
if ( ! attributeConverterDefinition.isAutoApply() ) {
continue;
}
if ( areTypeMatch( attributeConverterDefinition.getEntityAttributeType(), propertyType ) ) {
return attributeConverterDefinition;
}

View File

@ -49,6 +49,7 @@ import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter;
import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry;
import org.hibernate.type.descriptor.sql.JdbcTypeJavaClassMappings;
@ -393,23 +394,12 @@ public class SimpleValue implements KeyValue {
* </li>
* </ul>
*
* For the JavaTypeDescriptor portion we simply resolve the "entity attribute representation" part of
// the AttributeConverter to resolve the corresponding descriptor. For the SqlTypeDescriptor portion we use the
// "database column representation" part of the AttributeConverter to resolve the "recommended" JDBC type-code
// and use that type-code to resolve the SqlTypeDescriptor to use.
*
*
* <p/>
*
* <p/>
* <p/>
* @return The built AttributeConverter -> Type adapter
*
* @todo : ultimately I want to see attributeConverterJavaType and attributeConverterJdbcTypeCode specify-able separately
* then we can "play them against each other" in terms of determining proper typing
*
* @todo : see if we already have previously built a custom on-the-fly BasicType for this AttributeConverter; see note below about caching
*
* @return The built AttributeConverter -> Type adapter
*/
@SuppressWarnings("unchecked")
private Type buildAttributeConverterTypeAdapter() {
@ -445,28 +435,10 @@ public class SimpleValue implements KeyValue {
intermediateJavaTypeDescriptor
);
// todo : cache the AttributeConverterTypeAdapter in case that AttributeConverter is applied multiple times.
final String name = "BasicType adapter for AttributeConverter<" + entityAttributeJavaType + "," + databaseColumnJavaType + ">";
final Type type = new AbstractSingleColumnStandardBasicType( sqlTypeDescriptorAdapter, entityAttributeJavaTypeDescriptor ) {
@Override
public String getName() {
return name;
}
};
log.debug( "Created : " + name );
// todo : cache the BasicType we just created in case that AttributeConverter is applied multiple times.
return type;
}
private Class extractType(TypeVariable typeVariable) {
java.lang.reflect.Type[] boundTypes = typeVariable.getBounds();
if ( boundTypes == null || boundTypes.length != 1 ) {
return null;
}
return (Class) boundTypes[0];
return new AttributeConverterTypeAdapter( sqlTypeDescriptorAdapter, entityAttributeJavaTypeDescriptor, name );
}
public boolean isTypeSpecified() {

View File

@ -0,0 +1,54 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.type.descriptor.converter;
import org.jboss.logging.Logger;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* @author Steve Ebersole
*/
public class AttributeConverterTypeAdapter extends AbstractSingleColumnStandardBasicType {
private static final Logger log = Logger.getLogger( AttributeConverterTypeAdapter.class );
private final String name;
public AttributeConverterTypeAdapter(
SqlTypeDescriptor sqlTypeDescriptor,
JavaTypeDescriptor javaTypeDescriptor,
String name) {
super( sqlTypeDescriptor, javaTypeDescriptor );
this.name = name;
log.debug( "Created AttributeConverterTypeAdapter -> " + name );
}
@Override
public String getName() {
return name;
}
}

View File

@ -45,6 +45,7 @@ import org.hibernate.mapping.SimpleValue;
import org.hibernate.type.AbstractStandardBasicType;
import org.hibernate.type.BasicType;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter;
import org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
@ -54,8 +55,10 @@ import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
/**
* Tests the principle of adding "AttributeConverter" to the mix of {@link org.hibernate.type.Type} resolution
@ -80,6 +83,23 @@ public class AttributeConverterTest extends BaseUnitTestCase {
assertEquals( Types.CLOB, basicType.getSqlTypeDescriptor().getSqlType() );
}
@Test
public void testNonAutoApplyHandling() {
Configuration cfg = new Configuration();
cfg.addAttributeConverter( NotAutoAppliedConverter.class, false );
cfg.addAnnotatedClass( Tester.class );
cfg.buildMappings();
PersistentClass tester = cfg.getClassMapping( Tester.class.getName() );
Property nameProp = tester.getProperty( "name" );
SimpleValue nameValue = (SimpleValue) nameProp.getValue();
Type type = nameValue.getType();
assertNotNull( type );
if ( AttributeConverterTypeAdapter.class.isInstance( type ) ) {
fail( "AttributeConverter with autoApply=false was auto applied" );
}
}
@Test
public void testBasicConverterApplication() {
Configuration cfg = new Configuration();
@ -307,6 +327,19 @@ public class AttributeConverterTest extends BaseUnitTestCase {
// Converter declarations used in the test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Converter(autoApply = false)
public static class NotAutoAppliedConverter implements AttributeConverter<String,String> {
@Override
public String convertToDatabaseColumn(String attribute) {
throw new IllegalStateException( "AttributeConverter should not have been applied/called" );
}
@Override
public String convertToEntityAttribute(String dbData) {
throw new IllegalStateException( "AttributeConverter should not have been applied/called" );
}
}
@Converter( autoApply = true )
public static class StringClobConverter implements AttributeConverter<String,Clob> {
@Override

View File

@ -0,0 +1,115 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.test.convert;
import javax.persistence.AttributeConverter;
import javax.persistence.Convert;
import javax.persistence.Converter;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* Tests for asserting correct behavior of applying AttributeConverters explicitly listed in persistence.xml.
*
* @author Steve Ebersole
*/
public class ExplicitlyNamedConverterClassesTest extends BaseUnitTestCase {
// test handling of explicitly named, but non-auto-applied converter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Converter(autoApply = false)
public static class NotAutoAppliedConverter implements AttributeConverter<String,String> {
@Override
public String convertToDatabaseColumn(String attribute) {
throw new IllegalStateException( "AttributeConverter should not have been applied/called" );
}
@Override
public String convertToEntityAttribute(String dbData) {
throw new IllegalStateException( "AttributeConverter should not have been applied/called" );
}
}
@Entity( name = "Entity1" )
public static class Entity1 {
@Id
private Integer id;
private String name;
public Entity1() {
}
public Entity1(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@Test
public void testNonAutoAppliedConvertIsNotApplied() {
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
@Override
public List<String> getManagedClassNames() {
return Arrays.asList( Entity1.class.getName(), NotAutoAppliedConverter.class.getName() );
}
};
final Map settings = new HashMap();
settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build();
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist( new Entity1( 1, "1" ) );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery( "delete Entity1" ).executeUpdate();
em.getTransaction().commit();
em.close();
}
finally {
emf.close();
}
}
}