HHH-8529 - AttributeConverter not applied to @ElementCollection

This commit is contained in:
Steve Ebersole 2013-09-21 14:06:57 -05:00
parent 4d5174f55f
commit 4473f0302f
9 changed files with 786 additions and 15 deletions

View File

@ -124,7 +124,7 @@ public abstract class AbstractPropertyHolder implements PropertyHolder {
return null; return null;
} }
private AttributeConverterDefinition makeAttributeConverterDefinition(AttributeConversionInfo conversion) { protected AttributeConverterDefinition makeAttributeConverterDefinition(AttributeConversionInfo conversion) {
try { try {
return new AttributeConverterDefinition( conversion.getConverterClass().newInstance(), false ); return new AttributeConverterDefinition( conversion.getConverterClass().newInstance(), false );
} }
@ -133,7 +133,7 @@ public abstract class AbstractPropertyHolder implements PropertyHolder {
} }
} }
private boolean areTypeMatch(Class converterDefinedType, Class propertyType) { protected boolean areTypeMatch(Class converterDefinedType, Class propertyType) {
if ( converterDefinedType == null ) { if ( converterDefinedType == null ) {
throw new AnnotationException( "AttributeConverter defined java type cannot be null" ); throw new AnnotationException( "AttributeConverter defined java type cannot be null" );
} }

View File

@ -29,13 +29,10 @@ import javax.persistence.JoinTable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.jboss.logging.Logger;
import org.hibernate.annotations.common.AssertionFailure; import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.cfg.annotations.EntityBinder; import org.hibernate.cfg.annotations.EntityBinder;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.Component; import org.hibernate.mapping.Component;
import org.hibernate.mapping.Join; import org.hibernate.mapping.Join;
@ -47,6 +44,7 @@ import org.hibernate.mapping.Table;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public class ClassPropertyHolder extends AbstractPropertyHolder { public class ClassPropertyHolder extends AbstractPropertyHolder {
private PersistentClass persistentClass; private PersistentClass persistentClass;

View File

@ -23,11 +23,29 @@
*/ */
package org.hibernate.cfg; package org.hibernate.cfg;
import javax.persistence.Convert;
import javax.persistence.Converts;
import javax.persistence.Enumerated;
import javax.persistence.JoinTable; import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MapKeyClass;
import javax.persistence.MapKeyEnumerated;
import javax.persistence.MapKeyTemporal;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.annotations.CollectionType;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.Collection; import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Join; import org.hibernate.mapping.Join;
import org.hibernate.mapping.KeyValue; import org.hibernate.mapping.KeyValue;
@ -37,9 +55,19 @@ import org.hibernate.mapping.Table;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
* @author Steve Ebersole
*/ */
public class CollectionPropertyHolder extends AbstractPropertyHolder { public class CollectionPropertyHolder extends AbstractPropertyHolder {
Collection collection; private static final CoreMessageLogger log = CoreLogging.messageLogger( CollectionPropertyHolder.class );
private final Collection collection;
// assume true, the constructor will opt out where appropriate
private boolean canElementBeConverted = true;
private boolean canKeyBeConverted = true;
private Map<String,AttributeConversionInfo> elementAttributeConversionInfoMap;
private Map<String,AttributeConversionInfo> keyAttributeConversionInfoMap;
public CollectionPropertyHolder( public CollectionPropertyHolder(
Collection collection, Collection collection,
@ -51,6 +79,102 @@ public class CollectionPropertyHolder extends AbstractPropertyHolder {
super( path, parentPropertyHolder, clazzToProcess, mappings ); super( path, parentPropertyHolder, clazzToProcess, mappings );
this.collection = collection; this.collection = collection;
setCurrentProperty( property ); setCurrentProperty( property );
this.elementAttributeConversionInfoMap = new HashMap<String, AttributeConversionInfo>();
this.keyAttributeConversionInfoMap = new HashMap<String, AttributeConversionInfo>();
}
private void buildAttributeConversionInfoMaps(
XProperty collectionProperty,
Map<String,AttributeConversionInfo> elementAttributeConversionInfoMap,
Map<String,AttributeConversionInfo> keyAttributeConversionInfoMap) {
if ( collectionProperty == null ) {
// not sure this is valid condition
return;
}
{
final Convert convertAnnotation = collectionProperty.getAnnotation( Convert.class );
if ( convertAnnotation != null ) {
applyLocalConvert( convertAnnotation, collectionProperty, elementAttributeConversionInfoMap, keyAttributeConversionInfoMap );
}
}
{
final Converts convertsAnnotation = collectionProperty.getAnnotation( Converts.class );
if ( convertsAnnotation != null ) {
for ( Convert convertAnnotation : convertsAnnotation.value() ) {
applyLocalConvert(
convertAnnotation,
collectionProperty,
elementAttributeConversionInfoMap,
keyAttributeConversionInfoMap
);
}
}
}
}
private void applyLocalConvert(
Convert convertAnnotation,
XProperty collectionProperty,
Map<String,AttributeConversionInfo> elementAttributeConversionInfoMap,
Map<String,AttributeConversionInfo> keyAttributeConversionInfoMap) {
// IMPL NOTE : the rules here are quite more lenient than what JPA says. For example, JPA says
// that @Convert on a Map always needs to specify attributeName of key/value (or prefixed with
// key./value. for embedded paths). However, we try to see if conversion of either is disabled
// for whatever reason. For example, if the Map is annotated with @Enumerated the elements cannot
// be converted so any @Convert likely meant the key, so we apply it to the key
final AttributeConversionInfo info = new AttributeConversionInfo( convertAnnotation, collectionProperty );
if ( collection.isMap() ) {
boolean specCompliant = StringHelper.isNotEmpty( info.getAttributeName() )
&& ( info.getAttributeName().startsWith( "key" )
|| info.getAttributeName().startsWith( "value" ) );
if ( !specCompliant ) {
log.nonCompliantMapConversion( collection.getRole() );
}
}
if ( StringHelper.isEmpty( info.getAttributeName() ) ) {
if ( canElementBeConverted && canKeyBeConverted ) {
throw new IllegalStateException(
"@Convert placed on Map attribute [" + collection.getRole()
+ "] must define attributeName of 'key' or 'value'"
);
}
else if ( canKeyBeConverted ) {
keyAttributeConversionInfoMap.put( "", info );
}
else if ( canElementBeConverted ) {
elementAttributeConversionInfoMap.put( "", info );
}
// if neither, we should not be here...
}
else {
if ( canElementBeConverted && canKeyBeConverted ) {
// specified attributeName needs to have 'key.' or 'value.' prefix
if ( info.getAttributeName().startsWith( "key." ) ) {
keyAttributeConversionInfoMap.put(
info.getAttributeName().substring( 4 ),
info
);
}
else if ( info.getAttributeName().startsWith( "value." ) ) {
elementAttributeConversionInfoMap.put(
info.getAttributeName().substring( 6 ),
info
);
}
else {
throw new IllegalStateException(
"@Convert placed on Map attribute [" + collection.getRole()
+ "] must define attributeName of 'key' or 'value'"
);
}
}
}
} }
@Override @Override
@ -65,12 +189,69 @@ public class CollectionPropertyHolder extends AbstractPropertyHolder {
@Override @Override
public void startingProperty(XProperty property) { public void startingProperty(XProperty property) {
// todo : implement if ( property == null ) {
return;
}
// todo : implement (and make sure it gets called - for handling collections of composites)
}
public AttributeConverterDefinition resolveElementAttributeConverterDefinition(XClass elementXClass) {
AttributeConversionInfo info = locateAttributeConversionInfo( "element" );
if ( info != null ) {
if ( info.isConversionDisabled() ) {
return null;
}
else {
try {
return makeAttributeConverterDefinition( info );
}
catch (Exception e) {
throw new IllegalStateException(
String.format( "Unable to instantiate AttributeConverter [%s", info.getConverterClass().getName() ),
e
);
}
}
}
log.debugf(
"Attempting to locate auto-apply AttributeConverter for collection element [%s]",
collection.getRole()
);
final Class elementClass = getMappings().getReflectionManager().toClass( elementXClass );
for ( AttributeConverterDefinition attributeConverterDefinition : getMappings().getAttributeConverters() ) {
if ( ! attributeConverterDefinition.isAutoApply() ) {
continue;
}
log.debugf(
"Checking auto-apply AttributeConverter [%s] type [%s] for match [%s]",
attributeConverterDefinition.toString(),
attributeConverterDefinition.getEntityAttributeType().getSimpleName(),
elementClass.getSimpleName()
);
if ( areTypeMatch( attributeConverterDefinition.getEntityAttributeType(), elementClass ) ) {
return attributeConverterDefinition;
}
}
return null;
} }
@Override @Override
protected AttributeConversionInfo locateAttributeConversionInfo(XProperty property) { protected AttributeConversionInfo locateAttributeConversionInfo(XProperty property) {
// todo : implement if ( canElementBeConverted && canKeyBeConverted ) {
// need to decide whether 'property' refers to key/element
// todo : this may not work for 'basic collections' since there is no XProperty for the element
}
else if ( canKeyBeConverted ) {
}
else {
return null;
}
return null; return null;
} }
@ -133,4 +314,62 @@ public class CollectionPropertyHolder extends AbstractPropertyHolder {
public String toString() { public String toString() {
return super.toString() + "(" + collection.getRole() + ")"; return super.toString() + "(" + collection.getRole() + ")";
} }
boolean prepared;
public void prepare(XProperty collectionProperty) {
// fugly
if ( prepared ) {
return;
}
if ( collectionProperty == null ) {
return;
}
prepared = true;
if ( collection.isMap() ) {
if ( collectionProperty.isAnnotationPresent( MapKeyEnumerated.class ) ) {
canKeyBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( MapKeyTemporal.class ) ) {
canKeyBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( MapKeyClass.class ) ) {
canKeyBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( MapKeyType.class ) ) {
canKeyBeConverted = false;
}
}
else {
canKeyBeConverted = false;
}
if ( collectionProperty.isAnnotationPresent( ManyToAny.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( OneToMany.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( ManyToMany.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( Enumerated.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( Temporal.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( CollectionType.class ) ) {
canElementBeConverted = false;
}
// Is it valid to reference a collection attribute in a @Convert attached to the owner (entity) by path?
// if so we should pass in 'clazzToProcess' also
if ( canKeyBeConverted || canElementBeConverted ) {
buildAttributeConversionInfoMaps( collectionProperty, elementAttributeConversionInfoMap, keyAttributeConversionInfoMap );
}
}
} }

View File

@ -72,7 +72,7 @@ public final class PropertyHolderBuilder {
/** /**
* buid a property holder on top of a collection * buid a property holder on top of a collection
*/ */
public static PropertyHolder buildPropertyHolder( public static CollectionPropertyHolder buildPropertyHolder(
Collection collection, Collection collection,
String path, String path,
XClass clazzToProcess, XClass clazzToProcess,

View File

@ -84,6 +84,7 @@ import org.hibernate.cfg.AccessType;
import org.hibernate.cfg.AnnotatedClassType; import org.hibernate.cfg.AnnotatedClassType;
import org.hibernate.cfg.AnnotationBinder; import org.hibernate.cfg.AnnotationBinder;
import org.hibernate.cfg.BinderHelper; import org.hibernate.cfg.BinderHelper;
import org.hibernate.cfg.CollectionPropertyHolder;
import org.hibernate.cfg.CollectionSecondPass; import org.hibernate.cfg.CollectionSecondPass;
import org.hibernate.cfg.Ejb3Column; import org.hibernate.cfg.Ejb3Column;
import org.hibernate.cfg.Ejb3JoinColumn; import org.hibernate.cfg.Ejb3JoinColumn;
@ -1269,7 +1270,7 @@ public abstract class CollectionBinder {
XClass elementClass; XClass elementClass;
AnnotatedClassType classType; AnnotatedClassType classType;
PropertyHolder holder = null; CollectionPropertyHolder holder = null;
if ( BinderHelper.PRIMITIVE_NAMES.contains( collType.getName() ) ) { if ( BinderHelper.PRIMITIVE_NAMES.contains( collType.getName() ) ) {
classType = AnnotatedClassType.NONE; classType = AnnotatedClassType.NONE;
elementClass = null; elementClass = null;
@ -1282,11 +1283,20 @@ public abstract class CollectionBinder {
collValue, collValue,
collValue.getRole(), collValue.getRole(),
elementClass, elementClass,
property, parentPropertyHolder, mappings property,
parentPropertyHolder,
mappings
); );
// 'parentPropertyHolder' is the PropertyHolder for the owner of the collection
// 'holder' is the CollectionPropertyHolder.
// 'property' is the collection XProperty
parentPropertyHolder.startingProperty( property );
//force in case of attribute override //force in case of attribute override
boolean attributeOverride = property.isAnnotationPresent( AttributeOverride.class ) boolean attributeOverride = property.isAnnotationPresent( AttributeOverride.class )
|| property.isAnnotationPresent( AttributeOverrides.class ); || property.isAnnotationPresent( AttributeOverrides.class );
// todo : force in the case of Convert annotation(s) with embedded paths (beyond key/value prefixes)?
if ( isEmbedded || attributeOverride ) { if ( isEmbedded || attributeOverride ) {
classType = AnnotatedClassType.EMBEDDABLE; classType = AnnotatedClassType.EMBEDDABLE;
} }
@ -1329,10 +1339,18 @@ public abstract class CollectionBinder {
} }
} }
//TODO be smart with isNullable //TODO be smart with isNullable
boolean isNullable = true;
Component component = AnnotationBinder.fillComponent( Component component = AnnotationBinder.fillComponent(
holder, inferredData, isPropertyAnnotated ? AccessType.PROPERTY : AccessType.FIELD, true, holder,
entityBinder, false, false, inferredData,
true, mappings, inheritanceStatePerClass isPropertyAnnotated ? AccessType.PROPERTY : AccessType.FIELD,
isNullable,
entityBinder,
false,
false,
true,
mappings,
inheritanceStatePerClass
); );
collValue.setElement( component ); collValue.setElement( component );
@ -1346,6 +1364,8 @@ public abstract class CollectionBinder {
} }
} }
else { else {
holder.prepare( property );
SimpleValueBinder elementBinder = new SimpleValueBinder(); SimpleValueBinder elementBinder = new SimpleValueBinder();
elementBinder.setMappings( mappings ); elementBinder.setMappings( mappings );
elementBinder.setReturnedClassName( collType.getName() ); elementBinder.setReturnedClassName( collType.getName() );
@ -1368,7 +1388,12 @@ public abstract class CollectionBinder {
column.setTable( collValue.getCollectionTable() ); column.setTable( collValue.getCollectionTable() );
} }
elementBinder.setColumns( elementColumns ); elementBinder.setColumns( elementColumns );
elementBinder.setType( property, elementClass, collValue.getOwnerEntityName(), null ); elementBinder.setType(
property,
elementClass,
collValue.getOwnerEntityName(),
holder.resolveElementAttributeConverterDefinition( elementClass )
);
elementBinder.setPersistentClassName( propertyHolder.getEntityName() ); elementBinder.setPersistentClassName( propertyHolder.getEntityName() );
elementBinder.setAccessType( accessType ); elementBinder.setAccessType( accessType );
collValue.setElement( elementBinder.make() ); collValue.setElement( elementBinder.make() );

View File

@ -1621,4 +1621,12 @@ public interface CoreMessageLogger extends BasicLogger {
@LogMessage(level = INFO) @LogMessage(level = INFO)
@Message( value = "'javax.persistence.validation.mode' named multiple values : %s", id = 448 ) @Message( value = "'javax.persistence.validation.mode' named multiple values : %s", id = 448 )
void multipleValidationModes(String modes); void multipleValidationModes(String modes);
@LogMessage(level = WARN)
@Message(
id = 449,
value = "@Convert annotation applied to Map attribute [%s] did not explicitly specify attributeName " +
"using 'key'/'value' as required by spec; attempting to DoTheRightThing"
)
void nonCompliantMapConversion(String collectionRole);
} }

View File

@ -0,0 +1,171 @@
/*
* 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.CollectionTable;
import javax.persistence.Column;
import javax.persistence.Converter;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-8529" )
public class CollectionElementConversionTest extends BaseUnitTestCase {
@Test
public void testElementCollectionConversion() {
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
@Override
public List<String> getManagedClassNames() {
return Arrays.asList( Customer.class.getName(), ColorTypeConverter.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();
Customer customer = new Customer( 1 );
customer.colors.add( ColorType.BLUE );
em.persist( customer );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
assertEquals( 1, em.find( Customer.class, 1 ).colors.size() );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
customer = em.find( Customer.class, 1 );
em.remove( customer );
em.getTransaction().commit();
em.close();
}
finally {
emf.close();
}
}
@Entity( name = "Customer" )
@Table( name = "CUST" )
public static class Customer {
@Id
private Integer id;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
name = "cust_color",
joinColumns = @JoinColumn(name = "cust_fk", nullable = false),
uniqueConstraints = @UniqueConstraint(columnNames = { "customer_fk", "color" })
)
@Column(name = "color", nullable = false)
private Set<ColorType> colors = new HashSet<ColorType>();
public Customer() {
}
public Customer(Integer id) {
this.id = id;
}
}
// an enum-like class (converters are technically not allowed to apply to enums)
public static class ColorType {
public static ColorType BLUE = new ColorType( "blue" );
public static ColorType RED = new ColorType( "red" );
public static ColorType YELLOW = new ColorType( "yellow" );
private final String color;
public ColorType(String color) {
this.color = color;
}
public String toExternalForm() {
return color;
}
public static ColorType fromExternalForm(String color) {
if ( BLUE.color.equals( color ) ) {
return BLUE;
}
else if ( RED.color.equals( color ) ) {
return RED;
}
else if ( YELLOW.color.equals( color ) ) {
return YELLOW;
}
else {
throw new RuntimeException( "Unknown color : " + color );
}
}
}
@Converter( autoApply = true )
public static class ColorTypeConverter implements AttributeConverter<ColorType, String> {
@Override
public String convertToDatabaseColumn(ColorType attribute) {
return attribute == null ? null : attribute.toExternalForm();
}
@Override
public ColorType convertToEntityAttribute(String dbData) {
return ColorType.fromExternalForm( dbData );
}
}
}

View File

@ -0,0 +1,166 @@
/*
* 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.CollectionTable;
import javax.persistence.Column;
import javax.persistence.Converter;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import java.util.Arrays;
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.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-8529" )
public class MapElementConversionTest extends BaseUnitTestCase {
@Test
public void testElementCollectionConversion() {
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
@Override
public List<String> getManagedClassNames() {
return Arrays.asList( Customer.class.getName(), ColorTypeConverter.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();
Customer customer = new Customer( 1 );
customer.colors.put( "eyes", ColorType.BLUE );
em.persist( customer );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
assertEquals( 1, em.find( Customer.class, 1 ).colors.size() );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
customer = em.find( Customer.class, 1 );
em.remove( customer );
em.getTransaction().commit();
em.close();
}
finally {
emf.close();
}
}
@Entity( name = "Customer" )
@Table( name = "CUST" )
public static class Customer {
@Id
private Integer id;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable( name = "cust_color", joinColumns = @JoinColumn( name = "cust_fk" ) )
@MapKeyColumn( name = "color_key" )
@Column(name = "color", nullable = false)
private Map<String,ColorType> colors = new HashMap<String,ColorType>();
public Customer() {
}
public Customer(Integer id) {
this.id = id;
}
}
// an enum-like class (converters are technically not allowed to apply to enums)
public static class ColorType {
public static ColorType BLUE = new ColorType( "blue" );
public static ColorType RED = new ColorType( "red" );
public static ColorType YELLOW = new ColorType( "yellow" );
private final String color;
public ColorType(String color) {
this.color = color;
}
public String toExternalForm() {
return color;
}
public static ColorType fromExternalForm(String color) {
if ( BLUE.color.equals( color ) ) {
return BLUE;
}
else if ( RED.color.equals( color ) ) {
return RED;
}
else if ( YELLOW.color.equals( color ) ) {
return YELLOW;
}
else {
throw new RuntimeException( "Unknown color : " + color );
}
}
}
@Converter( autoApply = true )
public static class ColorTypeConverter implements AttributeConverter<ColorType, String> {
@Override
public String convertToDatabaseColumn(ColorType attribute) {
return attribute == null ? null : attribute.toExternalForm();
}
@Override
public ColorType convertToEntityAttribute(String dbData) {
return ColorType.fromExternalForm( dbData );
}
}
}

View File

@ -0,0 +1,164 @@
/*
* 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.CollectionTable;
import javax.persistence.Converter;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import java.util.Arrays;
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.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-8529" )
@FailureExpected( jiraKey = "HHH-8529" )
public class MapKeyConversionTest extends BaseUnitTestCase {
@Test
public void testElementCollectionConversion() {
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
@Override
public List<String> getManagedClassNames() {
return Arrays.asList( Customer.class.getName(), ColorTypeConverter.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();
Customer customer = new Customer( 1 );
customer.colors.put( ColorType.BLUE, "favorite" );
em.persist( customer );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
assertEquals( 1, em.find( Customer.class, 1 ).colors.size() );
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
customer = em.find( Customer.class, 1 );
em.remove( customer );
em.getTransaction().commit();
em.close();
}
finally {
emf.close();
}
}
@Entity( name = "Customer" )
@Table( name = "CUST" )
public static class Customer {
@Id
private Integer id;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable( name = "cust_color", joinColumns = @JoinColumn( name = "cust_fk" ) )
private Map<ColorType, String> colors = new HashMap<ColorType, String>();
public Customer() {
}
public Customer(Integer id) {
this.id = id;
}
}
// an enum-like class (converters are technically not allowed to apply to enums)
public static class ColorType {
public static ColorType BLUE = new ColorType( "blue" );
public static ColorType RED = new ColorType( "red" );
public static ColorType YELLOW = new ColorType( "yellow" );
private final String color;
public ColorType(String color) {
this.color = color;
}
public String toExternalForm() {
return color;
}
public static ColorType fromExternalForm(String color) {
if ( BLUE.color.equals( color ) ) {
return BLUE;
}
else if ( RED.color.equals( color ) ) {
return RED;
}
else if ( YELLOW.color.equals( color ) ) {
return YELLOW;
}
else {
throw new RuntimeException( "Unknown color : " + color );
}
}
}
@Converter( autoApply = true )
public static class ColorTypeConverter implements AttributeConverter<ColorType, String> {
@Override
public String convertToDatabaseColumn(ColorType attribute) {
return attribute == null ? null : attribute.toExternalForm();
}
@Override
public ColorType convertToEntityAttribute(String dbData) {
return ColorType.fromExternalForm( dbData );
}
}
}