From a2e844c381733ae4dc1915f0c88efa0a6bacf334 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 9 Mar 2012 11:47:57 -0800 Subject: [PATCH] HHH-7163 : Refactor code for binding PluralAttributeBinding --- .../hibernate/metamodel/internal/Binder.java | 410 ++++++++++++------ .../internal/HibernateTypeHelper.java | 201 +-------- .../AbstractPluralAttributeBinding.java | 11 - .../spi/binding/PluralAttributeBinding.java | 2 - .../binding/PluralAttributeKeyBinding.java | 12 +- .../AbstractCollectionPersister.java | 2 +- .../binding/BasicCollectionBindingTests.java | 41 +- 7 files changed, 336 insertions(+), 343 deletions(-) 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 94ee6c3118..f7ddd861da 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 @@ -30,6 +30,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; + import org.hibernate.EntityMode; import org.hibernate.TruthValue; import org.hibernate.cfg.AvailableSettings; @@ -45,6 +46,7 @@ import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding; import org.hibernate.metamodel.spi.binding.AttributeBinding; import org.hibernate.metamodel.spi.binding.AttributeBindingContainer; +import org.hibernate.metamodel.spi.binding.BagBinding; import org.hibernate.metamodel.spi.binding.BasicAttributeBinding; import org.hibernate.metamodel.spi.binding.BasicPluralAttributeElementBinding; import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding; @@ -56,7 +58,9 @@ 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.PluralAttributeKeyBinding; import org.hibernate.metamodel.spi.binding.RelationalValueBinding; import org.hibernate.metamodel.spi.binding.SecondaryTable; import org.hibernate.metamodel.spi.binding.SetBinding; @@ -100,7 +104,6 @@ import org.hibernate.metamodel.spi.source.MetaAttributeSource; import org.hibernate.metamodel.spi.source.MetadataImplementor; import org.hibernate.metamodel.spi.source.Orderable; -import org.hibernate.metamodel.spi.source.PluralAttributeElementSource; import org.hibernate.metamodel.spi.source.PluralAttributeKeySource; import org.hibernate.metamodel.spi.source.PluralAttributeNature; import org.hibernate.metamodel.spi.source.PluralAttributeSource; @@ -123,6 +126,7 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.service.config.spi.ConfigurationService; import org.hibernate.tuple.entity.EntityTuplizer; +import org.hibernate.type.ComponentType; import org.hibernate.type.EntityType; import org.hibernate.type.Type; @@ -193,10 +197,10 @@ private void bindAttributes( } } - private AbstractPluralAttributeBinding bindBagAttribute( + private AbstractPluralAttributeBinding createBagAttributeBinding( final AttributeBindingContainer attributeBindingContainer, final PluralAttributeSource attributeSource, - PluralAttribute attribute ) { + PluralAttribute attribute) { if ( attribute == null ) { attribute = attributeBindingContainer.getAttributeContainer().createBag( attributeSource.getName() ); } @@ -210,6 +214,40 @@ private AbstractPluralAttributeBinding bindBagAttribute( createMetaAttributeContext( attributeBindingContainer, attributeSource ) ); } + private Type createBagType(BagBinding bagBinding) { + if ( bagBinding.getHibernateTypeDescriptor().getExplicitTypeName() != null ) { + return resolveCustomCollectionType( bagBinding ); + } + else { + return metadata.getTypeResolver() + .getTypeFactory() + .bag( + bagBinding.getAttribute().getRole(), + bagBinding.getReferencedPropertyName(), + bagBinding.getPluralAttributeElementBinding() + .getPluralAttributeElementNature() == PluralAttributeElementNature.COMPOSITE + ); + } + } + + private Type resolveCustomCollectionType( PluralAttributeBinding pluralAttributeBinding) { + final HibernateTypeDescriptor hibernateTypeDescriptor = pluralAttributeBinding.getHibernateTypeDescriptor(); + Properties typeParameters = new Properties( ); + if ( hibernateTypeDescriptor.getTypeParameters() != null ) { + typeParameters.putAll( hibernateTypeDescriptor.getTypeParameters() ); + } + return metadata.getTypeResolver() + .getTypeFactory() + .customCollection( + hibernateTypeDescriptor.getExplicitTypeName(), + typeParameters, + pluralAttributeBinding.getAttribute().getName(), + pluralAttributeBinding.getReferencedPropertyName(), + pluralAttributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == + PluralAttributeElementNature.COMPOSITE + ); + } + private BasicAttributeBinding bindBasicAttribute( final AttributeBindingContainer attributeBindingContainer, final SingularAttributeSource attributeSource, @@ -234,22 +272,19 @@ private BasicAttributeBinding bindBasicAttribute( attributeSource.getGeneration() ); final HibernateTypeDescriptor hibernateTypeDescriptor = attributeBinding.getHibernateTypeDescriptor(); bindHibernateTypeDescriptor( - hibernateTypeDescriptor, - attributeSource.getTypeInformation(), - attributeBinding.getAttribute() - ); - resolveHibernateResolvedType( - attributeBinding.getHibernateTypeDescriptor(), - getHeuristicType( hibernateTypeDescriptor ), - ( AbstractValue ) relationalValueBindings.get( 0 ).getValue() + attributeBinding, + attributeSource.getTypeInformation() ); + Type resolvedType = getHeuristicType( hibernateTypeDescriptor ); + resolveHibernateResolvedType( attributeBinding.getHibernateTypeDescriptor(), resolvedType ); + resolveJdbcDataType( resolvedType,( AbstractValue ) relationalValueBindings.get( 0 ).getValue() ); attributeBinding.getAttribute().resolveType( bindingContexts.peek().makeJavaType( hibernateTypeDescriptor.getJavaTypeName() ) ); return attributeBinding; } private void bindBasicElementSetTablePrimaryKey( final SetBinding attributeBinding ) { - final PrimaryKey primaryKey = attributeBinding.getCollectionTable().getPrimaryKey(); + final PrimaryKey primaryKey = attributeBinding.getPluralAttributeKeyBinding().getCollectionTable().getPrimaryKey(); final ForeignKey foreignKey = attributeBinding.getPluralAttributeKeyBinding().getForeignKey(); final BasicPluralAttributeElementBinding elementBinding = ( BasicPluralAttributeElementBinding ) attributeBinding.getPluralAttributeElementBinding(); @@ -276,36 +311,38 @@ private void bindBasicElementSetTablePrimaryKey( final SetBinding attributeBindi private void bindBasicPluralElementRelationalValues( final RelationalValueSourceContainer relationalValueSourceContainer, final BasicPluralAttributeElementBinding elementBinding ) { - elementBinding.setRelationalValueBindings( bindValues( - elementBinding.getPluralAttributeBinding().getContainer(), - relationalValueSourceContainer, - elementBinding.getPluralAttributeBinding().getAttribute(), - elementBinding.getPluralAttributeBinding().getCollectionTable() ) ); + elementBinding.setRelationalValueBindings( + bindValues( + elementBinding.getPluralAttributeBinding().getContainer(), + relationalValueSourceContainer, + elementBinding.getPluralAttributeBinding().getAttribute(), + elementBinding.getPluralAttributeBinding().getPluralAttributeKeyBinding().getCollectionTable() + ) + ); } - private void bindCollectionElement( - final AbstractPluralAttributeBinding attributeBinding, - final PluralAttributeSource attributeSource ) { - final PluralAttributeElementSource elementSource = attributeSource.getElementSource(); - if ( elementSource.getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.BASIC ) { - final BasicPluralAttributeElementSource basicElementSource = ( BasicPluralAttributeElementSource ) elementSource; - final BasicPluralAttributeElementBinding basicCollectionElement = - ( BasicPluralAttributeElementBinding ) attributeBinding.getPluralAttributeElementBinding(); - bindBasicPluralElementRelationalValues( basicElementSource, basicCollectionElement ); - return; - } - // todo : handle cascades - // final Cascadeable cascadeable = (Cascadeable) binding.getPluralAttributeElementBinding(); - // cascadeable.setCascadeStyles( source.getCascadeStyles() ); - // todo : implement - throw new NotYetImplementedException( String.format( - "Support for collection elements of type %s not yet implemented", - elementSource.getNature() ) ); + private void bindBasicCollectionElement( + final BasicPluralAttributeElementBinding elementBinding, + final BasicPluralAttributeElementSource elementSource, + final String defaultElementJavaTypeName) { + bindBasicPluralElementRelationalValues( elementSource, elementBinding ); + bindHibernateTypeDescriptor( + elementBinding.getHibernateTypeDescriptor(), + elementSource.getExplicitHibernateTypeSource(), + defaultElementJavaTypeName + ); + Type resolvedElementType = getHeuristicType( elementBinding.getHibernateTypeDescriptor() ); + resolveHibernateResolvedType( elementBinding.getHibernateTypeDescriptor(), resolvedElementType ); + resolveJdbcDataType( + resolvedElementType, + ( AbstractValue ) elementBinding.getRelationalValueBindings().get( 0 ).getValue() + ); } private void bindCollectionIndex( final AbstractPluralAttributeBinding attributeBinding, - final PluralAttributeSource attributeSource ) { + final PluralAttributeSource attributeSource, + final String defaultElementJavaTypeNam) { if ( attributeSource.getPluralAttributeNature() != PluralAttributeNature.LIST && attributeSource.getPluralAttributeNature() != PluralAttributeNature.MAP ) { return; @@ -317,6 +354,22 @@ private void bindCollectionIndex( private void bindCollectionKey( final AbstractPluralAttributeBinding attributeBinding, final PluralAttributeSource attributeSource ) { + TableSpecification collectionTable; + if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.ONE_TO_MANY ) { + // TODO: Need to look up the table to be able to create the foreign key + throw new NotYetImplementedException( "one-to-many is not supported yet." ); + } + else { + collectionTable = createCollectionTable( attributeBinding, attributeSource ); + if ( StringHelper.isNotEmpty( attributeSource.getCollectionTableComment() ) ) { + collectionTable.addComment( attributeSource.getCollectionTableComment() ); + } + if ( StringHelper.isNotEmpty( attributeSource.getCollectionTableCheck() ) ) { + collectionTable.addCheckConstraint( attributeSource.getCollectionTableCheck() ); + } + } + + final PluralAttributeKeyBinding keyBinding = attributeBinding.getPluralAttributeKeyBinding(); final PluralAttributeKeySource keySource = attributeSource.getKeySource(); // todo: is null FK name allowed (is there a default?) final String foreignKeyName = @@ -324,23 +377,46 @@ private void bindCollectionKey( ? null : quotedIdentifier( keySource.getExplicitForeignKeyName() ); final TableSpecification table = attributeBinding.getContainer().seekEntityBinding().getPrimaryTable(); - attributeBinding.getPluralAttributeKeyBinding().prepareForeignKey( foreignKeyName, table ); - attributeBinding.getPluralAttributeKeyBinding().getForeignKey().setDeleteRule( keySource.getOnDeleteAction() ); + keyBinding.prepareForeignKey( foreignKeyName, collectionTable, table ); + keyBinding.getForeignKey().setDeleteRule( keySource.getOnDeleteAction() ); if ( keySource.getReferencedEntityAttributeName() == null ) { - bindCollectionKeyTargetingPrimaryKey( attributeBinding, attributeSource.getKeySource() ); + bindCollectionKeyTargetingPrimaryKey( attributeBinding, attributeSource.getKeySource(), collectionTable ); } else { - bindCollectionKeyTargetingPropertyRef( attributeBinding, attributeSource.getKeySource() ); + bindCollectionKeyTargetingPropertyRef( attributeBinding, attributeSource.getKeySource(), collectionTable ); + } + + final HibernateTypeDescriptor pluralAttributeKeyTypeDescriptor = keyBinding.getHibernateTypeDescriptor(); + final HibernateTypeDescriptor referencedTypeDescriptor = + keyBinding.getReferencedAttributeBinding().getHibernateTypeDescriptor(); + pluralAttributeKeyTypeDescriptor.setExplicitTypeName( referencedTypeDescriptor.getExplicitTypeName() ); + pluralAttributeKeyTypeDescriptor.setJavaTypeName( referencedTypeDescriptor.getJavaTypeName() ); + // TODO: not sure about the following... + pluralAttributeKeyTypeDescriptor.setToOne( referencedTypeDescriptor.isToOne() ); + pluralAttributeKeyTypeDescriptor.getTypeParameters().putAll( referencedTypeDescriptor.getTypeParameters() ); + final Type resolvedKeyType = referencedTypeDescriptor.getResolvedTypeMapping(); + pluralAttributeKeyTypeDescriptor.setResolvedTypeMapping( resolvedKeyType ); + + Iterator fkColumnIterator = keyBinding.getForeignKey().getSourceColumns().iterator(); + if ( resolvedKeyType.isComponentType() ) { + ComponentType componentType = ( ComponentType ) resolvedKeyType; + for ( Type subType : componentType.getSubtypes() ) { + resolveJdbcDataType( subType, fkColumnIterator.next() ); + } + } + else { + resolveJdbcDataType( resolvedKeyType, fkColumnIterator.next() ); } } private void bindCollectionKeyTargetingPrimaryKey( final AbstractPluralAttributeBinding attributeBinding, - final PluralAttributeKeySource keySource ) { + final PluralAttributeKeySource keySource, + final TableSpecification collectionTable) { for ( final RelationalValueSource valueSource : keySource.getValueSources() ) { if ( valueSource instanceof ColumnSource ) { final Column column = createColumn( - attributeBinding.getCollectionTable(), + collectionTable, ( ColumnSource ) valueSource, attributeBinding.getAttribute().getName(), false, @@ -354,7 +430,8 @@ private void bindCollectionKeyTargetingPrimaryKey( private void bindCollectionKeyTargetingPropertyRef( final AbstractPluralAttributeBinding attributeBinding, - final PluralAttributeKeySource keySource ) { + final PluralAttributeKeySource keySource, + final TableSpecification collectionTable ) { final EntityBinding ownerEntityBinding = attributeBinding.getContainer().seekEntityBinding(); final AttributeBinding referencedAttributeBinding = ownerEntityBinding.locateAttributeBinding( keySource.getReferencedEntityAttributeName() ); @@ -378,7 +455,7 @@ private void bindCollectionKeyTargetingPropertyRef( final ColumnSource columnSource = ( ColumnSource ) valueSource; final Column column = createColumn( - attributeBinding.getCollectionTable(), + collectionTable, columnSource, attributeBinding.getAttribute().getName(), false, @@ -404,12 +481,9 @@ private void bindCollectionKeyTargetingPropertyRef( } } - private void bindCollectionTable( + private TableSpecification createCollectionTable( final AbstractPluralAttributeBinding pluralAttributeBinding, - final PluralAttributeSource attributeSource ) { - if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.ONE_TO_MANY ) { - return; - } + final PluralAttributeSource attributeSource) { final DefaultNamingStrategy defaultNamingStategy = new DefaultNamingStrategy() { @Override @@ -428,25 +502,27 @@ public String defaultName() { attributeBindingContainer.getPathBase() + '.' + attributeSource.getName() ); } }; - pluralAttributeBinding.setCollectionTable( createTable( + return createTable( attributeSource.getCollectionTableSpecificationSource(), - defaultNamingStategy ) ); - if ( StringHelper.isNotEmpty( attributeSource.getCollectionTableComment() ) ) { - pluralAttributeBinding.getCollectionTable().addComment( attributeSource.getCollectionTableComment() ); - } - if ( StringHelper.isNotEmpty( attributeSource.getCollectionTableCheck() ) ) { - pluralAttributeBinding.getCollectionTable().addCheckConstraint( attributeSource.getCollectionTableCheck() ); - } + defaultNamingStategy ); } private void bindCollectionTablePrimaryKey( final AbstractPluralAttributeBinding attributeBinding, - final PluralAttributeSource attributeSource ) { + final PluralAttributeSource attributeSource, + final HibernateTypeHelper.ReflectedCollectionJavaTypes reflectedCollectionJavaTypes) { if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.ONE_TO_MANY || attributeSource.getPluralAttributeNature() == PluralAttributeNature.BAG ) { 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 { @@ -522,10 +598,9 @@ private void bindDiscriminator( final EntityBinding rootEntityBinding, final Roo : "string"; final HibernateTypeDescriptor hibernateTypeDescriptor = discriminator.getExplicitHibernateTypeDescriptor(); hibernateTypeDescriptor.setExplicitTypeName( typeName ); - resolveHibernateResolvedType( - hibernateTypeDescriptor, - getHeuristicType( hibernateTypeDescriptor ), - value ); + Type resolvedType = getHeuristicType( hibernateTypeDescriptor ); + resolveHibernateResolvedType( hibernateTypeDescriptor, resolvedType ); + resolveJdbcDataType( resolvedType, value ); } private EntityBinding bindEntities( final EntityHierarchy entityHierarchy ) { @@ -595,37 +670,54 @@ private EntityBinding bindEntity( final EntitySource entitySource, final EntityB } private void bindHibernateTypeDescriptor( - final HibernateTypeDescriptor hibernateTypeDescriptor, - final ExplicitHibernateTypeSource typeSource, - final Attribute attribute) { - String typeName = typeSource.getName(); - // Check if user specified a type - if ( typeName == null ) { - // Obtain Java type name from attribute - typeName = determineJavaType( attribute ).getName(); - hibernateTypeDescriptor.setJavaTypeName( typeName ); - } else { - // 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() ); - } - final Map< String, String > typeParameters = typeSource.getParameters(); - if ( typeParameters != null ) { - hibernateTypeDescriptor.getTypeParameters().putAll( typeParameters ); - } - } + 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() ); } + + private void bindHibernateTypeDescriptor( + final HibernateTypeDescriptor hibernateTypeDescriptor, + final ExplicitHibernateTypeSource explicitTypeSource, + final String defaultJavaTypeName) { + if ( explicitTypeSource.getName() == null ) { + if ( hibernateTypeDescriptor.getJavaTypeName() == null ) { + hibernateTypeDescriptor.setJavaTypeName( defaultJavaTypeName ); + } + } + else { + bindExplicitHibernateTypeDescriptor( + hibernateTypeDescriptor, explicitTypeSource.getName(), explicitTypeSource.getParameters() + ); + } + } private void bindIdentifier( final EntityBinding rootEntityBinding, final IdentifierSource identifierSource ) { final Nature nature = identifierSource.getNature(); @@ -694,15 +786,11 @@ private ManyToOneAttributeBinding bindManyToOneAttribute( ); // TODO: need to be able to deal with composite IDs bindHibernateTypeDescriptor( - attributeBinding.getHibernateTypeDescriptor(), - attributeSource.getTypeInformation(), - attributeBinding.getAttribute() - ); - resolveHibernateResolvedType( - attributeBinding.getHibernateTypeDescriptor(), - resolvedType, - (AbstractValue) relationalValueBindings.get( 0 ).getValue() + attributeBinding, + attributeSource.getTypeInformation() ); + resolveHibernateResolvedType( attributeBinding.getHibernateTypeDescriptor(), resolvedType ); + resolveJdbcDataType( resolvedType, (AbstractValue) relationalValueBindings.get( 0 ).getValue() ); attributeBinding.setCascadeStyles( attributeSource.getCascadeStyles() ); attributeBinding.setFetchTiming( attributeSource.getFetchTiming() ); attributeBinding.setFetchStyle( attributeSource.getFetchStyle() ); @@ -712,28 +800,50 @@ private ManyToOneAttributeBinding bindManyToOneAttribute( private AbstractPluralAttributeBinding bindPluralAttribute( final AttributeBindingContainer attributeBindingContainer, - final PluralAttributeSource attributeSource ) { + final PluralAttributeSource attributeSource) { final PluralAttributeNature nature = attributeSource.getPluralAttributeNature(); final PluralAttribute attribute = attributeBindingContainer.getAttributeContainer().locatePluralAttribute( attributeSource.getName() ); AbstractPluralAttributeBinding attributeBinding; + HibernateTypeHelper.ReflectedCollectionJavaTypes reflectedCollectionJavaTypes; if ( nature == PluralAttributeNature.BAG ) { - attributeBinding = bindBagAttribute( attributeBindingContainer, attributeSource, attribute ); + attributeBinding = createBagAttributeBinding( attributeBindingContainer, attributeSource, attribute ); + reflectedCollectionJavaTypes = typeHelper.getReflectedCollectionJavaTypes( attributeBinding ); + bindHibernateTypeDescriptor( + attributeBinding.getHibernateTypeDescriptor(), + attributeSource.getTypeInformation(), + getDefaultCollectionJavaTypeName( reflectedCollectionJavaTypes, attributeSource) + ); + resolveHibernateResolvedType( + attributeBinding.getHibernateTypeDescriptor(), + createBagType( (BagBinding) attributeBinding ) + ); + // collection types do not have a relational model } else if ( nature == PluralAttributeNature.SET ) { - attributeBinding = bindSetAttribute( attributeBindingContainer, attributeSource, attribute ); + attributeBinding = createSetAttributeBinding( attributeBindingContainer, attributeSource, attribute ); + reflectedCollectionJavaTypes = typeHelper.getReflectedCollectionJavaTypes( attributeBinding ); + bindHibernateTypeDescriptor( + attributeBinding.getHibernateTypeDescriptor(), + attributeSource.getTypeInformation(), + getDefaultCollectionJavaTypeName( reflectedCollectionJavaTypes, attributeSource) + ); + resolveHibernateResolvedType( + attributeBinding.getHibernateTypeDescriptor(), + createSetType( (SetBinding) attributeBinding ) + ); + // collection types do not have a relational model } else { throw new NotYetImplementedException( nature.toString() ); } attributeBinding.setFetchTiming( attributeSource.getFetchTiming() ); attributeBinding.setFetchStyle( attributeSource.getFetchStyle() ); attributeBinding.setCaching( attributeSource.getCaching() ); - attributeBinding.getHibernateTypeDescriptor().setJavaTypeName( nature.reportedJavaType().getName() ); - attributeBinding.getHibernateTypeDescriptor().setExplicitTypeName( attributeSource.getTypeInformation().getName() ); - attributeBinding.getHibernateTypeDescriptor().getTypeParameters().putAll( - attributeSource.getTypeInformation().getParameters() ); if ( StringHelper.isNotEmpty( attributeSource.getCustomPersisterClassName() ) ) { - attributeBinding.setExplicitPersisterClass( bindingContexts.peek().< CollectionPersister >locateClassByName( - attributeSource.getCustomPersisterClassName() ) ); + attributeBinding.setExplicitPersisterClass( + bindingContexts.peek().locateClassByName( + attributeSource.getCustomPersisterClassName() + ) + ); } attributeBinding.setCustomLoaderName( attributeSource.getCustomLoaderName() ); attributeBinding.setCustomSqlInsert( attributeSource.getCustomSqlInsert() ); @@ -741,16 +851,56 @@ private AbstractPluralAttributeBinding bindPluralAttribute( attributeBinding.setCustomSqlDelete( attributeSource.getCustomSqlDelete() ); attributeBinding.setCustomSqlDeleteAll( attributeSource.getCustomSqlDeleteAll() ); attributeBinding.setWhere( attributeSource.getWhere() ); - bindCollectionTable( attributeBinding, attributeSource ); + bindSortingAndOrdering( attributeBinding, attributeSource ); + bindCollectionKey( attributeBinding, attributeSource ); - bindCollectionElement( attributeBinding, attributeSource ); - bindCollectionIndex( attributeBinding, attributeSource ); - bindCollectionTablePrimaryKey( attributeBinding, attributeSource ); - typeHelper.bindPluralAttributeTypeInformation( attributeSource, attributeBinding ); + + if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.BASIC ) { + bindBasicCollectionElement( + (BasicPluralAttributeElementBinding) attributeBinding.getPluralAttributeElementBinding(), + (BasicPluralAttributeElementSource) attributeSource.getElementSource(), + getDefaultCollectionElementJavaTypeName( reflectedCollectionJavaTypes ) + ); + } + else { + throw new NotYetImplementedException( String.format( + "Support for collection elements of type %s not yet implemented", + attributeSource.getElementSource().getNature() ) ); + } + + bindCollectionIndex( + attributeBinding, + attributeSource, + getDefaultCollectionIndexJavaTypeName( reflectedCollectionJavaTypes ) + ); + + bindCollectionTablePrimaryKey( attributeBinding, attributeSource, reflectedCollectionJavaTypes ); metadata.addCollection( attributeBinding ); return attributeBinding; } + + private String getDefaultCollectionJavaTypeName( + HibernateTypeHelper.ReflectedCollectionJavaTypes reflectedCollectionJavaTypes, + PluralAttributeSource attributeSource) { + return reflectedCollectionJavaTypes != null && reflectedCollectionJavaTypes.getCollectionType() != null ? + reflectedCollectionJavaTypes.getCollectionType().getName() : + attributeSource.getPluralAttributeNature().reportedJavaType().getName(); + } + + private String getDefaultCollectionElementJavaTypeName( + HibernateTypeHelper.ReflectedCollectionJavaTypes reflectedCollectionJavaTypes) { + return reflectedCollectionJavaTypes != null && reflectedCollectionJavaTypes.getCollectionElementType() != null ? + reflectedCollectionJavaTypes.getCollectionElementType().getName() : + null; + } + + private String getDefaultCollectionIndexJavaTypeName( + HibernateTypeHelper.ReflectedCollectionJavaTypes reflectedCollectionJavaTypes) { + return reflectedCollectionJavaTypes != null && reflectedCollectionJavaTypes.getCollectionIndexType() != null ? + reflectedCollectionJavaTypes.getCollectionIndexType().getName() : + null; + } private void bindPrimaryTable( final EntityBinding entityBinding, final EntitySource entitySource ) { entityBinding.setPrimaryTable( createTable( entitySource.getPrimaryTable(), new DefaultNamingStrategy() { @@ -799,10 +949,10 @@ private void bindSecondaryTables( final EntityBinding entityBinding, final Entit } } - private AbstractPluralAttributeBinding bindSetAttribute( + private AbstractPluralAttributeBinding createSetAttributeBinding( final AttributeBindingContainer attributeBindingContainer, final PluralAttributeSource attributeSource, - PluralAttribute attribute ) { + PluralAttribute attribute) { if ( attribute == null ) { attribute = attributeBindingContainer.getAttributeContainer().createSet( attributeSource.getName() ); } @@ -817,6 +967,22 @@ private AbstractPluralAttributeBinding bindSetAttribute( null ); } + private Type createSetType(SetBinding bagBinding) { + if ( bagBinding.getHibernateTypeDescriptor().getExplicitTypeName() != null ) { + return resolveCustomCollectionType( bagBinding ); + } + else { + return metadata.getTypeResolver() + .getTypeFactory() + .set( + bagBinding.getAttribute().getRole(), + bagBinding.getReferencedPropertyName(), + bagBinding.getPluralAttributeElementBinding() + .getPluralAttributeElementNature() == PluralAttributeElementNature.COMPOSITE + ); + } + } + private void bindSimpleIdentifier( final EntityBinding rootEntityBinding, final SimpleIdentifierSource identifierSource ) { final BasicAttributeBinding idAttributeBinding = ( BasicAttributeBinding ) bindAttribute( rootEntityBinding, identifierSource.getIdentifierAttributeSource() ); @@ -1152,7 +1318,8 @@ private TableSpecification createTable( final InLineViewSource inLineViewSource = ( InLineViewSource ) tableSpecSource; return schema.createInLineView( Identifier.toIdentifier( inLineViewSource.getLogicalName() ), - inLineViewSource.getSelectStatement() ); + inLineViewSource.getSelectStatement() + ); } private EntityBinding entityBinding( final String entityName ) { @@ -1221,23 +1388,24 @@ private String quotedIdentifier( final String name ) { private void resolveHibernateResolvedType( final HibernateTypeDescriptor hibernateTypeDescriptor, - final Type resolvedType, - final AbstractValue value ) { + final Type resolvedType) { // Configure relational value JDBC type from Hibernate type descriptor now that its configured if ( resolvedType != null ) { hibernateTypeDescriptor.setResolvedTypeMapping( resolvedType ); if ( hibernateTypeDescriptor.getJavaTypeName() == null ) { hibernateTypeDescriptor.setJavaTypeName( resolvedType.getReturnedClass().getName() ); } - Type resolvedRelationalType; - if ( resolvedType.isEntityType() ) { - resolvedRelationalType = EntityType.class.cast( resolvedType ).getIdentifierOrUniqueKeyType( metadata ); - hibernateTypeDescriptor.setToOne( true ); - } - else { - resolvedRelationalType = resolvedType; - hibernateTypeDescriptor.setToOne( false ); - } + hibernateTypeDescriptor.setToOne( resolvedType.isEntityType() ); + } + } + + private void resolveJdbcDataType( + final Type resolvedType, + final AbstractValue value) { + if ( resolvedType != null && value != null ) { + final Type resolvedRelationalType = resolvedType.isEntityType() ? + EntityType.class.cast( resolvedType ).getIdentifierOrUniqueKeyType( metadata ) : + resolvedType; value.setJdbcDataType( new JdbcDataType( resolvedRelationalType.sqlTypes( metadata )[ 0 ], resolvedRelationalType.getName(), 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 d0c52f3cac..5ccf80b356 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 @@ -34,42 +34,30 @@ import org.jboss.logging.Logger; import org.hibernate.AssertionFailure; -import org.hibernate.engine.FetchTiming; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.beans.BeanInfoHelper; -import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding; import org.hibernate.metamodel.spi.binding.AttributeBinding; import org.hibernate.metamodel.spi.binding.BasicAttributeBinding; -import org.hibernate.metamodel.spi.binding.BasicPluralAttributeElementBinding; import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding; import org.hibernate.metamodel.spi.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.spi.binding.IndexedPluralAttributeBinding; import org.hibernate.metamodel.spi.binding.PluralAttributeBinding; -import org.hibernate.metamodel.spi.binding.PluralAttributeElementBinding; -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.SingularAttributeBinding; import org.hibernate.metamodel.spi.binding.TypeDefinition; import org.hibernate.metamodel.spi.domain.PluralAttribute; import org.hibernate.metamodel.spi.domain.SingularAttribute; import org.hibernate.metamodel.spi.relational.AbstractValue; -import org.hibernate.metamodel.spi.relational.Column; import org.hibernate.metamodel.spi.relational.JdbcDataType; import org.hibernate.metamodel.spi.relational.Value; import org.hibernate.metamodel.spi.source.AttributeSource; -import org.hibernate.metamodel.spi.source.BasicPluralAttributeElementSource; import org.hibernate.metamodel.spi.source.ComponentAttributeSource; import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource; import org.hibernate.metamodel.spi.source.MetadataImplementor; -import org.hibernate.metamodel.spi.source.PluralAttributeElementSource; -import org.hibernate.metamodel.spi.source.PluralAttributeSource; import org.hibernate.metamodel.spi.source.SingularAttributeSource; -import org.hibernate.type.ComponentType; import org.hibernate.type.EntityType; import org.hibernate.type.Type; -import org.hibernate.type.TypeFactory; /** * Delegate for handling:
    @@ -88,7 +76,6 @@ *

    * Methods intended as entry points are:

      *
    • {@link #bindSingularAttributeTypeInformation}
    • - *
    • {@link #bindPluralAttributeTypeInformation}
    • *
    *

    * Currently the following methods are also required to be non-private because of handling discriminators which @@ -129,13 +116,12 @@ public void bindSingularAttributeTypeInformation( } } - bindHibernateTypeInformation( attributeSource.getTypeInformation(), false, hibernateTypeDescriptor ); + bindHibernateTypeInformation( attributeSource.getTypeInformation(), hibernateTypeDescriptor ); processSingularAttributeTypeInformation( attributeSource, attributeBinding ); } - public void bindPluralAttributeTypeInformation( - PluralAttributeSource attributeSource, + public ReflectedCollectionJavaTypes getReflectedCollectionJavaTypes( PluralAttributeBinding attributeBinding) { final ReflectedCollectionJavaTypes reflectedCollectionJavaTypes = determineJavaType( attributeBinding.getAttribute() ); @@ -165,57 +151,7 @@ public void bindPluralAttributeTypeInformation( } } } - - bindHibernateTypeInformation( - attributeSource.getTypeInformation(), - false, - attributeBinding.getHibernateTypeDescriptor() - ); - processPluralAttributeTypeInformation( attributeSource, attributeBinding ); - } - - private void processPluralAttributeKeyTypeInformation(PluralAttributeKeyBinding keyBinding) { - final HibernateTypeDescriptor pluralAttributeKeyTypeDescriptor = keyBinding.getHibernateTypeDescriptor(); - final HibernateTypeDescriptor referencedTypeDescriptor = - keyBinding.getReferencedAttributeBinding().getHibernateTypeDescriptor(); - - pluralAttributeKeyTypeDescriptor.setExplicitTypeName( referencedTypeDescriptor.getExplicitTypeName() ); - pluralAttributeKeyTypeDescriptor.setJavaTypeName( referencedTypeDescriptor.getJavaTypeName() ); - - // TODO: not sure about the following... - pluralAttributeKeyTypeDescriptor.setToOne( referencedTypeDescriptor.isToOne() ); - pluralAttributeKeyTypeDescriptor.getTypeParameters().putAll( referencedTypeDescriptor.getTypeParameters() ); - - processPluralAttributeKeyInformation( keyBinding ); - } - - private void processPluralAttributeKeyInformation(PluralAttributeKeyBinding keyBinding) { - if ( keyBinding.getHibernateTypeDescriptor().getResolvedTypeMapping() != null ) { - return; - } - // we can determine the Hibernate Type if either: - // 1) the user explicitly named a Type in a HibernateTypeDescriptor - // 2) we know the java type of the attribute - Type resolvedType = determineHibernateTypeFromDescriptor( keyBinding.getHibernateTypeDescriptor() ); - if ( resolvedType == null ) { - resolvedType = determineHibernateTypeFromAttributeJavaType( - keyBinding.getReferencedAttributeBinding().getAttribute() - ); - } - - if ( resolvedType != null ) { - Iterator fkColumnIterator = keyBinding.getForeignKey().getSourceColumns().iterator(); - - if ( resolvedType.isComponentType() ) { - ComponentType componentType = ( ComponentType ) resolvedType; - for ( Type subType : componentType.getSubtypes() ) { - pushHibernateTypeInformationDown( subType, fkColumnIterator.next() ); - } - } - else { - pushHibernateTypeInformationDown( resolvedType, fkColumnIterator.next() ); - } - } + return reflectedCollectionJavaTypes; } private Class determineJavaType(final SingularAttribute attribute) { @@ -257,12 +193,10 @@ private ReflectedCollectionJavaTypes determineJavaType(PluralAttribute attribute * Takes explicit source type information and applies it to the binding model. * * @param typeSource The source (user supplied) hibernate type information - * @param isToOne Indicates if this for a to-one association * @param hibernateTypeDescriptor The binding model hibernate type information */ private void bindHibernateTypeInformation( ExplicitHibernateTypeSource typeSource, - boolean isToOne, HibernateTypeDescriptor hibernateTypeDescriptor) { final String explicitTypeName = typeSource.getName(); if ( explicitTypeName != null ) { @@ -279,7 +213,6 @@ private void bindHibernateTypeInformation( hibernateTypeDescriptor.getTypeParameters().putAll( parameters ); } } - hibernateTypeDescriptor.setToOne( isToOne ); } /** @@ -414,12 +347,6 @@ private void pushHibernateTypeInformationDown( SingularAttributeBinding.class.cast( subAttributeBinding ) ); } - else if ( AbstractPluralAttributeBinding.class.isInstance( subAttributeBinding ) ) { - processPluralAttributeTypeInformation( - ( PluralAttributeSource ) subAttributeSource, - AbstractPluralAttributeBinding.class.cast( subAttributeBinding ) - ); - } else { throw new AssertionFailure( "Unknown type of AttributeBinding: " + attributeBinding.getClass().getName() ); } @@ -473,116 +400,8 @@ public void pushHibernateTypeInformationDown(Type resolvedHibernateType, Value v } } - private void processPluralAttributeTypeInformation( - PluralAttributeSource attributeSource, - PluralAttributeBinding attributeBinding) { - processCollectionTypeInformation( attributeBinding ); - processPluralAttributeElementTypeInformation( attributeSource.getElementSource(), attributeBinding.getPluralAttributeElementBinding() ); - processPluralAttributeKeyTypeInformation( attributeBinding.getPluralAttributeKeyBinding() ); - } - - private void processCollectionTypeInformation(PluralAttributeBinding attributeBinding) { - if ( attributeBinding.getHibernateTypeDescriptor().getResolvedTypeMapping() != null ) { - return; - } - - Type resolvedType; - // do NOT look at java type... - //String typeName = determineTypeName( attributeBinding.getHibernateTypeDescriptor() ); - String typeName = attributeBinding.getHibernateTypeDescriptor().getExplicitTypeName(); - if ( typeName != null ) { - resolvedType = - metadata.getTypeResolver() - .getTypeFactory() - .customCollection( - typeName, - getTypeParameters( attributeBinding.getHibernateTypeDescriptor() ), - attributeBinding.getAttribute().getName(), - attributeBinding.getReferencedPropertyName(), - attributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == - PluralAttributeElementNature.COMPOSITE - ); - } - else { - resolvedType = determineHibernateTypeFromCollectionType( attributeBinding ); - } - if ( resolvedType != null ) { - attributeBinding.getHibernateTypeDescriptor().setResolvedTypeMapping( resolvedType ); - } - } - - private Type determineHibernateTypeFromCollectionType(PluralAttributeBinding attributeBinding) { - final TypeFactory typeFactory = metadata.getTypeResolver().getTypeFactory(); - switch ( attributeBinding.getAttribute().getNature() ) { - case SET: { - return typeFactory.set( - attributeBinding.getAttribute().getRole(), - attributeBinding.getReferencedPropertyName(), - attributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == PluralAttributeElementNature.COMPOSITE - ); - } - case BAG: { - return typeFactory.bag( - attributeBinding.getAttribute().getRole(), - attributeBinding.getReferencedPropertyName(), - attributeBinding.getPluralAttributeElementBinding() - .getPluralAttributeElementNature() == PluralAttributeElementNature.COMPOSITE - ); - } - default: { - throw new UnsupportedOperationException( - "Collection type not supported yet:" + attributeBinding.getAttribute().getNature() - ); - } - } - } - - private void processPluralAttributeElementTypeInformation( - PluralAttributeElementSource elementSource, - PluralAttributeElementBinding pluralAttributeElementBinding - ) { - switch ( pluralAttributeElementBinding.getPluralAttributeElementNature() ) { - case BASIC: { - processBasicCollectionElementTypeInformation( - BasicPluralAttributeElementSource.class.cast( elementSource ), - BasicPluralAttributeElementBinding.class.cast( pluralAttributeElementBinding ) - ); - break; - } - case COMPOSITE: - case ONE_TO_MANY: - case MANY_TO_MANY: - case MANY_TO_ANY: { - throw new UnsupportedOperationException( "Collection element nature not supported yet: " + pluralAttributeElementBinding - .getPluralAttributeElementNature() ); - } - default: { - throw new AssertionFailure( "Unknown collection element nature : " + pluralAttributeElementBinding.getPluralAttributeElementNature() ); - } - } - } - - private void processBasicCollectionElementTypeInformation( - BasicPluralAttributeElementSource elementSource, - BasicPluralAttributeElementBinding basicCollectionElementBinding) { - Type resolvedType = basicCollectionElementBinding.getHibernateTypeDescriptor().getResolvedTypeMapping(); - if ( resolvedType == null ) { - bindHibernateTypeInformation( - elementSource.getExplicitHibernateTypeSource(), - false, - basicCollectionElementBinding.getHibernateTypeDescriptor() ); - resolvedType = determineHibernateTypeFromDescriptor( basicCollectionElementBinding.getHibernateTypeDescriptor() ); - } - if ( resolvedType != null ) { - pushHibernateTypeInformationDown( - basicCollectionElementBinding.getHibernateTypeDescriptor(), - basicCollectionElementBinding.getRelationalValueBindings(), - resolvedType - ); - } - } - - private static class ReflectedCollectionJavaTypes { + /* package-protected */ + static class ReflectedCollectionJavaTypes { private final Class collectionType; private final Class collectionElementType; private final Class collectionIndexType; @@ -595,6 +414,16 @@ private ReflectedCollectionJavaTypes( this.collectionElementType = collectionElementType; this.collectionIndexType = collectionIndexType; } + + public Class getCollectionType() { + return collectionType; + } + public Class getCollectionElementType() { + return collectionElementType; + } + public Class getCollectionIndexType() { + return collectionIndexType; + } } /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractPluralAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractPluralAttributeBinding.java index df026e7d9c..061053abbd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractPluralAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/AbstractPluralAttributeBinding.java @@ -45,8 +45,6 @@ public abstract class AbstractPluralAttributeBinding extends AbstractAttributeBi private final PluralAttributeKeyBinding pluralAttributeKeyBinding; private final AbstractPluralAttributeElementBinding pluralAttributeElementBinding; - private TableSpecification collectionTable; - private FetchTiming fetchTiming; private FetchStyle fetchStyle; @@ -162,15 +160,6 @@ public boolean isAssociation() { return pluralAttributeElementBinding.getPluralAttributeElementNature().isAssociation(); } - @Override - public TableSpecification getCollectionTable() { - return collectionTable; - } - - public void setCollectionTable(TableSpecification collectionTable) { - this.collectionTable = collectionTable; - } - @Override public PluralAttributeKeyBinding getPluralAttributeKeyBinding() { return pluralAttributeKeyBinding; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeBinding.java index fa1550fda3..39255a8897 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeBinding.java @@ -57,8 +57,6 @@ public interface PluralAttributeBinding extends AttributeBinding, Fetchable { */ public PluralAttributeElementBinding getPluralAttributeElementBinding(); - public TableSpecification getCollectionTable(); - public boolean isMutable(); public Caching getCaching(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeKeyBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeKeyBinding.java index aeb1783fd2..dd854d664f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeKeyBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeKeyBinding.java @@ -75,6 +75,10 @@ public ForeignKey getForeignKey() { return foreignKey; } + public TableSpecification getCollectionTable() { + return foreignKey.getSourceTable(); + } + /** * Is the plural attribute considered inverse? *

    @@ -94,13 +98,15 @@ public HibernateTypeDescriptor getHibernateTypeDescriptor() { return hibernateTypeDescriptor; } - public void prepareForeignKey(String foreignKeyName, TableSpecification targetTable) { + public void prepareForeignKey( + String foreignKeyName, + TableSpecification collectionTable, + TableSpecification targetTable) { if ( foreignKey != null ) { throw new AssertionFailure( "Foreign key already initialized" ); } - final TableSpecification collectionTable = pluralAttributeBinding.getCollectionTable(); if ( collectionTable == null ) { - throw new AssertionFailure( "Collection table not yet bound" ); + throw new AssertionFailure( "Collection table cannot be null" ); } if ( foreignKeyName != null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index 5fb81cd944..614c1ed1dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -654,7 +654,7 @@ public AbstractCollectionPersister( nodeName = null; isMutable = collection.isMutable(); - TableSpecification table = collection.getCollectionTable(); + TableSpecification table = collection.getPluralAttributeKeyBinding().getCollectionTable(); fetchMode = collection.getFetchMode(); elementType = collection.getPluralAttributeElementBinding().getHibernateTypeDescriptor().getResolvedTypeMapping(); // isSet = collection.isSet(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/BasicCollectionBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/BasicCollectionBindingTests.java index bb00754f80..9221361ca6 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/BasicCollectionBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/BasicCollectionBindingTests.java @@ -24,7 +24,6 @@ package org.hibernate.metamodel.spi.binding; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.Set; @@ -38,6 +37,7 @@ import org.hibernate.metamodel.spi.relational.Column; import org.hibernate.metamodel.spi.relational.ForeignKey; import org.hibernate.metamodel.spi.relational.Identifier; +import org.hibernate.metamodel.spi.relational.TableSpecification; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.internal.StandardServiceRegistryImpl; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -93,8 +93,9 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { PluralAttributeBinding bagBinding = metadata.getCollection( EntityWithBasicCollections.class.getName() + ".theBag" ); assertNotNull( bagBinding ); assertSame( bagBinding, entityBinding.locateAttributeBinding( "theBag" ) ); - assertNotNull( bagBinding.getCollectionTable() ); - assertEquals( Identifier.toIdentifier( "`EntityWithBasicCollections_theBag`" ), bagBinding.getCollectionTable().getLogicalName() ); + TableSpecification bagCollectionTable = bagBinding.getPluralAttributeKeyBinding().getCollectionTable(); + assertNotNull( bagCollectionTable ); + assertEquals( Identifier.toIdentifier( "`EntityWithBasicCollections_theBag`" ), bagCollectionTable.getLogicalName() ); PluralAttributeKeyBinding bagKeyBinding = bagBinding.getPluralAttributeKeyBinding(); assertSame( bagBinding, bagKeyBinding.getPluralAttributeBinding() ); HibernateTypeDescriptor bagHibernateTypeDescriptor = bagBinding.getHibernateTypeDescriptor(); @@ -107,7 +108,7 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { ForeignKey fkBag = bagKeyBinding.getForeignKey(); assertNotNull( fkBag ); - assertSame( bagBinding.getCollectionTable(), fkBag.getSourceTable() ); + assertSame( bagCollectionTable, fkBag.getSourceTable() ); assertEquals( 1, fkBag.getColumnSpan() ); Iterator fkBagColumnIterator = fkBag.getColumns().iterator(); Iterator fkBagSourceColumnIterator = fkBag.getSourceColumns().iterator(); @@ -129,7 +130,7 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { entityIdentifier.getValueBinding().getHibernateTypeDescriptor(), bagKeyBinding.getHibernateTypeDescriptor() ); - assertEquals( 0, bagBinding.getCollectionTable().getPrimaryKey().getColumnSpan() ); + assertEquals( 0, bagCollectionTable.getPrimaryKey().getColumnSpan() ); assertEquals( entityBinding.getPrimaryTable().getPrimaryKey().getColumns().iterator().next().getJdbcDataType(), bagKeyBinding.getForeignKey().getColumns().iterator().next().getJdbcDataType() @@ -141,8 +142,9 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { PluralAttributeBinding setBinding = metadata.getCollection( EntityWithBasicCollections.class.getName() + ".theSet" ); assertNotNull( setBinding ); assertSame( setBinding, entityBinding.locateAttributeBinding( "theSet" ) ); - assertNotNull( setBinding.getCollectionTable() ); - assertEquals( Identifier.toIdentifier( "`EntityWithBasicCollections_theSet`" ), setBinding.getCollectionTable().getLogicalName() ); + TableSpecification setCollectionTable = setBinding.getPluralAttributeKeyBinding().getCollectionTable(); + assertNotNull( setCollectionTable ); + assertEquals( Identifier.toIdentifier( "`EntityWithBasicCollections_theSet`" ), setCollectionTable.getLogicalName() ); PluralAttributeKeyBinding setKeyBinding = setBinding.getPluralAttributeKeyBinding(); assertSame( setBinding, setKeyBinding.getPluralAttributeBinding() ); HibernateTypeDescriptor setHibernateTypeDescriptor = setBinding.getHibernateTypeDescriptor(); @@ -155,7 +157,7 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { ForeignKey fkSet = setKeyBinding.getForeignKey(); assertNotNull( fkSet ); - assertSame( setBinding.getCollectionTable(), fkSet.getSourceTable() ); + assertSame( setCollectionTable, fkSet.getSourceTable() ); assertEquals( 1, fkSet.getColumnSpan() ); Iterator fkSetColumnIterator = fkSet.getColumns().iterator(); Iterator fkSetSourceColumnIterator = fkSet.getSourceColumns().iterator(); @@ -178,19 +180,19 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { setKeyBinding.getHibernateTypeDescriptor() ); assertFalse( setKeyBinding.isInverse() ); - assertEquals( 2, setBinding.getCollectionTable().getPrimaryKey().getColumnSpan() ); - Iterator setPrimaryKeyIterator = setBinding.getCollectionTable().getPrimaryKey().getColumns().iterator(); + assertEquals( 2, setCollectionTable.getPrimaryKey().getColumnSpan() ); + Iterator setPrimaryKeyIterator = setCollectionTable.getPrimaryKey().getColumns().iterator(); assertEquals( entityBinding.getPrimaryTable().getPrimaryKey().getColumns().iterator().next().getJdbcDataType(), setPrimaryKeyIterator.next().getJdbcDataType() ); assertEquals( - setBinding.getCollectionTable().locateColumn( "`set_stuff`" ).getJdbcDataType(), + setCollectionTable.locateColumn( "`set_stuff`" ).getJdbcDataType(), setPrimaryKeyIterator.next().getJdbcDataType() ); assertFalse( setPrimaryKeyIterator.hasNext() ); assertSame( - setBinding.getCollectionTable().getPrimaryKey().getColumns().iterator().next(), + setCollectionTable.getPrimaryKey().getColumns().iterator().next(), setKeyBinding.getForeignKey().getColumns().iterator().next() ); assertEquals( PluralAttributeElementNature.BASIC, setBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() ); @@ -199,8 +201,9 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { PluralAttributeBinding propertyRefSetBinding = metadata.getCollection( EntityWithBasicCollections.class.getName() + ".thePropertyRefSet" ); assertNotNull( propertyRefSetBinding ); assertSame( propertyRefSetBinding, entityBinding.locateAttributeBinding( "thePropertyRefSet" ) ); - assertNotNull( propertyRefSetBinding.getCollectionTable() ); - assertEquals( Identifier.toIdentifier( "`EntityWithBasicCollections_thePropertyRefSet`" ), propertyRefSetBinding.getCollectionTable().getLogicalName() ); + TableSpecification propertyRefSetCollectionTable = propertyRefSetBinding.getPluralAttributeKeyBinding().getCollectionTable(); + assertNotNull( propertyRefSetCollectionTable ); + assertEquals( Identifier.toIdentifier( "`EntityWithBasicCollections_thePropertyRefSet`" ), propertyRefSetCollectionTable.getLogicalName() ); PluralAttributeKeyBinding propertyRefSetKeyBinding = propertyRefSetBinding.getPluralAttributeKeyBinding(); assertSame( propertyRefSetBinding, propertyRefSetKeyBinding.getPluralAttributeBinding() ); HibernateTypeDescriptor propertyRefSetHibernateTypeDescriptor = propertyRefSetBinding.getHibernateTypeDescriptor(); @@ -216,7 +219,7 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { ForeignKey fkPropertyRefSet = propertyRefSetKeyBinding.getForeignKey(); assertNotNull( fkPropertyRefSet ); - assertSame( propertyRefSetBinding.getCollectionTable(), fkPropertyRefSet.getSourceTable() ); + assertSame( propertyRefSetCollectionTable, fkPropertyRefSet.getSourceTable() ); assertEquals( 1, fkPropertyRefSet.getColumnSpan() ); Iterator fkPropertyRefSetColumnIterator = fkPropertyRefSet.getColumns().iterator(); Iterator fkPropertyRefSetSourceColumnIterator = fkPropertyRefSet.getSourceColumns().iterator(); @@ -239,19 +242,19 @@ private void doTest(MetadataSourceProcessingOrder processingOrder) { propertyRefSetKeyBinding.getHibernateTypeDescriptor() ); assertFalse( propertyRefSetKeyBinding.isInverse() ); - assertEquals( 2, propertyRefSetBinding.getCollectionTable().getPrimaryKey().getColumnSpan() ); - Iterator propertyRefSetPrimaryKeyIterator = propertyRefSetBinding.getCollectionTable().getPrimaryKey().getColumns().iterator(); + assertEquals( 2, propertyRefSetCollectionTable.getPrimaryKey().getColumnSpan() ); + Iterator propertyRefSetPrimaryKeyIterator = propertyRefSetCollectionTable.getPrimaryKey().getColumns().iterator(); assertEquals( entityBinding.getPrimaryTable().locateColumn( "`name`" ).getJdbcDataType(), propertyRefSetPrimaryKeyIterator.next().getJdbcDataType() ); assertEquals( - propertyRefSetBinding.getCollectionTable().locateColumn( "`property_ref_set_stuff`" ).getJdbcDataType(), + propertyRefSetCollectionTable.locateColumn( "`property_ref_set_stuff`" ).getJdbcDataType(), propertyRefSetPrimaryKeyIterator.next().getJdbcDataType() ); assertFalse( propertyRefSetPrimaryKeyIterator.hasNext() ); assertSame( - propertyRefSetBinding.getCollectionTable().getPrimaryKey().getColumns().iterator().next(), + propertyRefSetCollectionTable.getPrimaryKey().getColumns().iterator().next(), propertyRefSetKeyBinding.getForeignKey().getColumns().iterator().next() ); assertEquals( PluralAttributeElementNature.BASIC, propertyRefSetBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() );