HHH-8529 - AttributeConverter not applied to @ElementCollection

This commit is contained in:
Steve Ebersole 2013-09-21 16:55:05 -05:00
parent 4473f0302f
commit a1ff3a29cb
3 changed files with 54 additions and 14 deletions

View File

@ -220,25 +220,56 @@ public class CollectionPropertyHolder extends AbstractPropertyHolder {
collection.getRole() collection.getRole()
); );
final Class elementClass = getMappings().getReflectionManager().toClass( elementXClass ); final Class elementClass = determineElementClass( elementXClass );
for ( AttributeConverterDefinition attributeConverterDefinition : getMappings().getAttributeConverters() ) { if ( elementClass != null ) {
if ( ! attributeConverterDefinition.isAutoApply() ) { for ( AttributeConverterDefinition attributeConverterDefinition : getMappings().getAttributeConverters() ) {
continue; if ( ! attributeConverterDefinition.isAutoApply() ) {
} continue;
log.debugf( }
"Checking auto-apply AttributeConverter [%s] type [%s] for match [%s]", log.debugf(
attributeConverterDefinition.toString(), "Checking auto-apply AttributeConverter [%s] type [%s] for match [%s]",
attributeConverterDefinition.getEntityAttributeType().getSimpleName(), attributeConverterDefinition.toString(),
elementClass.getSimpleName() attributeConverterDefinition.getEntityAttributeType().getSimpleName(),
); elementClass.getSimpleName()
if ( areTypeMatch( attributeConverterDefinition.getEntityAttributeType(), elementClass ) ) { );
return attributeConverterDefinition; if ( areTypeMatch( attributeConverterDefinition.getEntityAttributeType(), elementClass ) ) {
return attributeConverterDefinition;
}
} }
} }
return null; 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()
);
}
}
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;
}
@Override @Override
protected AttributeConversionInfo locateAttributeConversionInfo(XProperty property) { protected AttributeConversionInfo locateAttributeConversionInfo(XProperty property) {
if ( canElementBeConverted && canKeyBeConverted ) { if ( canElementBeConverted && canKeyBeConverted ) {

View File

@ -1274,6 +1274,15 @@ public abstract class CollectionBinder {
if ( BinderHelper.PRIMITIVE_NAMES.contains( collType.getName() ) ) { if ( BinderHelper.PRIMITIVE_NAMES.contains( collType.getName() ) ) {
classType = AnnotatedClassType.NONE; classType = AnnotatedClassType.NONE;
elementClass = null; elementClass = null;
holder = PropertyHolderBuilder.buildPropertyHolder(
collValue,
collValue.getRole(),
null,
property,
parentPropertyHolder,
mappings
);
} }
else { else {
elementClass = collType; elementClass = collType;

View File

@ -109,7 +109,7 @@ public class CollectionElementConversionTest extends BaseUnitTestCase {
@CollectionTable( @CollectionTable(
name = "cust_color", name = "cust_color",
joinColumns = @JoinColumn(name = "cust_fk", nullable = false), joinColumns = @JoinColumn(name = "cust_fk", nullable = false),
uniqueConstraints = @UniqueConstraint(columnNames = { "customer_fk", "color" }) uniqueConstraints = @UniqueConstraint(columnNames = { "cust_fk", "color" })
) )
@Column(name = "color", nullable = false) @Column(name = "color", nullable = false)
private Set<ColorType> colors = new HashSet<ColorType>(); private Set<ColorType> colors = new HashSet<ColorType>();