diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java index f7ddd861da..3fe1dc5615 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/Binder.java @@ -55,11 +55,13 @@ import org.hibernate.metamodel.spi.binding.EntityDiscriminator; import org.hibernate.metamodel.spi.binding.EntityVersion; import org.hibernate.metamodel.spi.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.spi.binding.IdGenerator; +import org.hibernate.metamodel.spi.binding.IndexedPluralAttributeBinding; import org.hibernate.metamodel.spi.binding.InheritanceType; import org.hibernate.metamodel.spi.binding.ManyToOneAttributeBinding; import org.hibernate.metamodel.spi.binding.MetaAttribute; import org.hibernate.metamodel.spi.binding.PluralAttributeBinding; import org.hibernate.metamodel.spi.binding.PluralAttributeElementNature; +import org.hibernate.metamodel.spi.binding.PluralAttributeIndexBinding; import org.hibernate.metamodel.spi.binding.PluralAttributeKeyBinding; import org.hibernate.metamodel.spi.binding.RelationalValueBinding; import org.hibernate.metamodel.spi.binding.SecondaryTable; @@ -272,8 +274,9 @@ public class Binder { attributeSource.getGeneration() ); final HibernateTypeDescriptor hibernateTypeDescriptor = attributeBinding.getHibernateTypeDescriptor(); bindHibernateTypeDescriptor( - attributeBinding, - attributeSource.getTypeInformation() + attributeBinding.getHibernateTypeDescriptor(), + attributeSource.getTypeInformation(), + createSingularAttributeJavaType( attributeBinding.getAttribute() ) ); Type resolvedType = getHeuristicType( hibernateTypeDescriptor ); resolveHibernateResolvedType( attributeBinding.getHibernateTypeDescriptor(), resolvedType ); @@ -342,11 +345,12 @@ public class Binder { private void bindCollectionIndex( final AbstractPluralAttributeBinding attributeBinding, final PluralAttributeSource attributeSource, - final String defaultElementJavaTypeNam) { - if ( attributeSource.getPluralAttributeNature() != PluralAttributeNature.LIST && - attributeSource.getPluralAttributeNature() != PluralAttributeNature.MAP ) { + final String defaultElementJavaTypeName) { + if ( ! IndexedPluralAttributeBinding.class.isInstance( attributeBinding ) ) { return; } + final PluralAttributeIndexBinding indexBinding + = ( (IndexedPluralAttributeBinding) attributeBinding ).getPluralAttributeIndexBinding(); // todo : implement throw new NotYetImplementedException(); } @@ -516,13 +520,6 @@ public class Binder { return; } if ( attributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == PluralAttributeElementNature.BASIC ) { - bindHibernateTypeDescriptor( - attributeBinding.getPluralAttributeElementBinding().getHibernateTypeDescriptor(), - ( (BasicPluralAttributeElementSource) attributeSource.getElementSource() ).getExplicitHibernateTypeSource(), - reflectedCollectionJavaTypes != null && reflectedCollectionJavaTypes.getCollectionElementType() != null ? - reflectedCollectionJavaTypes.getCollectionElementType().getName() : - null - ); if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.SET ) { bindBasicElementSetTablePrimaryKey( ( SetBinding ) attributeBinding ); } else { @@ -670,36 +667,15 @@ public class Binder { } private void bindHibernateTypeDescriptor( - final SingularAttributeBinding singularAttributeBinding, - final ExplicitHibernateTypeSource typeSource) { - bindHibernateTypeDescriptor( - singularAttributeBinding.getHibernateTypeDescriptor(), - typeSource, - determineJavaType( singularAttributeBinding.getAttribute() ).getName() - ); - } - - private void bindExplicitHibernateTypeDescriptor( final HibernateTypeDescriptor hibernateTypeDescriptor, - final String typeName, - final Map typeParameters) { - // Check if user-specified name is of a User-Defined Type (UDT) - final TypeDefinition typeDef = metadata.getTypeDefinition( typeName ); - if ( typeDef == null ) { - hibernateTypeDescriptor.setExplicitTypeName( typeName ); - } else { - hibernateTypeDescriptor.setExplicitTypeName( typeDef.getTypeImplementorClass().getName() ); - hibernateTypeDescriptor.setTypeParameters( typeDef.getParameters() ); - } - if ( typeParameters != null ) { - hibernateTypeDescriptor.getTypeParameters().putAll( typeParameters ); - } - } - - private Class determineJavaType(Attribute attribute) { - return ReflectHelper.reflectedPropertyClass( - attribute.getAttributeContainer().getClassReference(), - attribute.getName() + final ExplicitHibernateTypeSource explicitTypeSource, + final org.hibernate.internal.util.Value> defaultJavaType) { + // if there is an explicit type name specified, then there's no reason to + // initialize the default Java type name; simply pass a null default instead. + bindHibernateTypeDescriptor( + hibernateTypeDescriptor, + explicitTypeSource, + explicitTypeSource.getName() == null ? defaultJavaType.getValue().getName() : null ); } @@ -708,17 +684,59 @@ public class Binder { final ExplicitHibernateTypeSource explicitTypeSource, final String defaultJavaTypeName) { if ( explicitTypeSource.getName() == null ) { - if ( hibernateTypeDescriptor.getJavaTypeName() == null ) { - hibernateTypeDescriptor.setJavaTypeName( defaultJavaTypeName ); + if ( hibernateTypeDescriptor.getJavaTypeName() != null ) { + throw new MappingException( + String.format( + "Attempt to re-initialize (non-explicit) Java type name; current=%s new=%s", + hibernateTypeDescriptor.getJavaTypeName(), + defaultJavaTypeName + ), + bindingContexts.peek().getOrigin() + ); } + hibernateTypeDescriptor.setJavaTypeName( defaultJavaTypeName ); } else { - bindExplicitHibernateTypeDescriptor( - hibernateTypeDescriptor, explicitTypeSource.getName(), explicitTypeSource.getParameters() - ); + final String typeName = explicitTypeSource.getName(); + final Map typeParameters = explicitTypeSource.getParameters(); + // Check if user-specified name is of a User-Defined Type (UDT) + final TypeDefinition typeDef = metadata.getTypeDefinition( typeName ); + if ( hibernateTypeDescriptor.getExplicitTypeName() != null ) { + throw new MappingException( + String.format( + "Attempt to re-initialize explicity-mapped Java type name; current=%s new=%s", + hibernateTypeDescriptor.getExplicitTypeName(), + typeName + ), + bindingContexts.peek().getOrigin() + ); + } + if ( typeDef == null ) { + hibernateTypeDescriptor.setExplicitTypeName( typeName ); + } else { + hibernateTypeDescriptor.setExplicitTypeName( typeDef.getTypeImplementorClass().getName() ); + hibernateTypeDescriptor.setTypeParameters( typeDef.getParameters() ); + } + if ( typeParameters != null ) { + hibernateTypeDescriptor.getTypeParameters().putAll( typeParameters ); + } } } - + + private static org.hibernate.internal.util.Value> createSingularAttributeJavaType( + final SingularAttribute attribute) { + org.hibernate.internal.util.Value.DeferredInitializer> deferredInitializer = + new org.hibernate.internal.util.Value.DeferredInitializer>() { + public Class initialize() { + return ReflectHelper.reflectedPropertyClass( + attribute.getAttributeContainer().getClassReference(), + attribute.getName() + ); + } + }; + return new org.hibernate.internal.util.Value>( deferredInitializer ); + } + private void bindIdentifier( final EntityBinding rootEntityBinding, final IdentifierSource identifierSource ) { final Nature nature = identifierSource.getNature(); if ( nature == Nature.SIMPLE ) { @@ -752,9 +770,11 @@ public class Binder { attributeSource, attribute, attributeBindingContainer.seekEntityBinding().getPrimaryTable() ); + org.hibernate.internal.util.Value> referencedJavaTypeValue = + createSingularAttributeJavaType( attribute ); final String referencedEntityName = attributeSource.getReferencedEntityName() != null ? attributeSource.getReferencedEntityName() - : determineJavaType( attribute ).getName(); + : referencedJavaTypeValue.getValue().getName(); final EntityBinding referencedEntityBinding = entityBinding( referencedEntityName ); final AttributeBinding referencedAttributeBinding = attributeSource.getReferencedEntityAttributeName() == null @@ -786,8 +806,9 @@ public class Binder { ); // TODO: need to be able to deal with composite IDs bindHibernateTypeDescriptor( - attributeBinding, - attributeSource.getTypeInformation() + attributeBinding.getHibernateTypeDescriptor(), + attributeSource.getTypeInformation(), + referencedJavaTypeValue ); resolveHibernateResolvedType( attributeBinding.getHibernateTypeDescriptor(), resolvedType ); resolveJdbcDataType( resolvedType, (AbstractValue) relationalValueBindings.get( 0 ).getValue() ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java index 5ccf80b356..fbb5db83f1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HibernateTypeHelper.java @@ -123,35 +123,7 @@ public class HibernateTypeHelper { public ReflectedCollectionJavaTypes getReflectedCollectionJavaTypes( PluralAttributeBinding attributeBinding) { - final ReflectedCollectionJavaTypes reflectedCollectionJavaTypes = determineJavaType( attributeBinding.getAttribute() ); - - if ( reflectedCollectionJavaTypes != null ) { - if ( reflectedCollectionJavaTypes.collectionType != null ) { - if ( attributeBinding.getHibernateTypeDescriptor().getJavaTypeName() == null ) { - attributeBinding.getHibernateTypeDescriptor().setJavaTypeName( - reflectedCollectionJavaTypes.collectionType.getName() - ); - } - } - if ( reflectedCollectionJavaTypes.collectionElementType != null ) { - if ( attributeBinding.getPluralAttributeElementBinding().getHibernateTypeDescriptor().getJavaTypeName() == null ) { - attributeBinding.getPluralAttributeElementBinding().getHibernateTypeDescriptor().setJavaTypeName( - reflectedCollectionJavaTypes.collectionElementType.getName() - ); - } - } - if ( reflectedCollectionJavaTypes.collectionIndexType != null - && IndexedPluralAttributeBinding.class.isInstance( attributeBinding ) ) { - final PluralAttributeIndexBinding indexBinding - = ( (IndexedPluralAttributeBinding) attributeBinding ).getPluralAttributeIndexBinding(); - if ( indexBinding.getHibernateTypeDescriptor().getJavaTypeName() == null ) { - indexBinding.getHibernateTypeDescriptor().setJavaTypeName( - reflectedCollectionJavaTypes.collectionIndexType.getName() - ); - } - } - } - return reflectedCollectionJavaTypes; + return determineJavaType( attributeBinding.getAttribute() ); } private Class determineJavaType(final SingularAttribute attribute) { @@ -414,7 +386,7 @@ public class HibernateTypeHelper { this.collectionElementType = collectionElementType; this.collectionIndexType = collectionIndexType; } - + public Class getCollectionType() { return collectionType; } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/EntityWithBasicCollections.hbm.xml b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/EntityWithBasicCollections.hbm.xml index f3f8ee0280..8eecf99eba 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/EntityWithBasicCollections.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/EntityWithBasicCollections.hbm.xml @@ -22,7 +22,7 @@ - +