diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/CollectionPropertyHolder.java b/hibernate-core/src/main/java/org/hibernate/cfg/CollectionPropertyHolder.java index 1b6df3cbd692..40ea8e81a0ee 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/CollectionPropertyHolder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/CollectionPropertyHolder.java @@ -220,22 +220,53 @@ public AttributeConverterDefinition resolveElementAttributeConverterDefinition(X collection.getRole() ); - final Class elementClass = getMappings().getReflectionManager().toClass( elementXClass ); - for ( AttributeConverterDefinition attributeConverterDefinition : getMappings().getAttributeConverters() ) { - if ( ! attributeConverterDefinition.isAutoApply() ) { - continue; + final Class elementClass = determineElementClass( elementXClass ); + if ( elementClass != null ) { + 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; + } + + private Class determineElementClass(XClass elementXClass) { + if ( elementXClass != null ) { + try { + return getMappings().getReflectionManager().toClass( elementXClass ); + } + catch (Exception e) { + log.debugf( + "Unable to resolve XClass [%s] to Class for collection elements [%s]", + elementXClass.getName(), + collection.getRole() + ); } - 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; + } + + if ( collection.getElement() != null ) { + if ( collection.getElement().getType() != null ) { + return collection.getElement().getType().getReturnedClass(); } } + // currently this is called from paths where the element type really should be known, + // so log the fact that we could not resolve the collection element info + log.debugf( + "Unable to resolve element information for collection [%s]", + collection.getRole() + ); return null; } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index dff787df1459..e72f657c4cd3 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -1274,6 +1274,15 @@ else if ( anyAnn != null ) { if ( BinderHelper.PRIMITIVE_NAMES.contains( collType.getName() ) ) { classType = AnnotatedClassType.NONE; elementClass = null; + + holder = PropertyHolderBuilder.buildPropertyHolder( + collValue, + collValue.getRole(), + null, + property, + parentPropertyHolder, + mappings + ); } else { elementClass = collType; diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java index e7098b379660..ee7e97ecdec4 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java @@ -109,7 +109,7 @@ public static class Customer { @CollectionTable( name = "cust_color", joinColumns = @JoinColumn(name = "cust_fk", nullable = false), - uniqueConstraints = @UniqueConstraint(columnNames = { "customer_fk", "color" }) + uniqueConstraints = @UniqueConstraint(columnNames = { "cust_fk", "color" }) ) @Column(name = "color", nullable = false) private Set colors = new HashSet();