From 41513dbb2c003484e6f1beaf5edf2b6750c14572 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 19 Jul 2013 12:45:41 -0700 Subject: [PATCH] HHH-8355 : Extract Binder code related to relational objects into helpers --- .../hibernate/metamodel/internal/Binder.java | 206 +++++++----------- .../internal/EntityHierarchyHelper.java | 152 +++---------- .../metamodel/internal/ForeignKeyHelper.java | 38 ++-- .../metamodel/internal/HelperContext.java | 39 ++++ .../internal/HibernateTypeHelper.java | 22 +- .../LocalBindingContextManagerImpl.java | 176 +++++++++++++++ .../internal/RelationalIdentifierHelper.java | 18 +- .../RelationalValueBindingHelper.java | 27 +-- .../metamodel/internal/TableHelper.java | 33 +-- ...ociationRelationalBindingResolverImpl.java | 39 ++-- ...ociationRelationalBindingResolverImpl.java | 52 +++-- 11 files changed, 457 insertions(+), 345 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/internal/HelperContext.java create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/internal/LocalBindingContextManagerImpl.java 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 001b1860c7..60637efa6e 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 @@ -44,7 +44,6 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NotYetImplementedException; -import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.engine.FetchStyle; import org.hibernate.engine.FetchTiming; import org.hibernate.engine.config.spi.ConfigurationService; @@ -63,7 +62,6 @@ import org.hibernate.internal.FilterConfiguration; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.ValueHolder; -import org.hibernate.jaxb.spi.Origin; import org.hibernate.metamodel.internal.EntityHierarchyHelper.LocalBindingContextExecutionContext; import org.hibernate.metamodel.internal.EntityHierarchyHelper.LocalBindingContextExecutor; import org.hibernate.metamodel.internal.HibernateTypeHelper.ReflectedCollectionJavaTypes; @@ -143,8 +141,6 @@ import org.hibernate.metamodel.spi.source.JoinedSubclassEntitySource; import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.ManyToManyPluralAttributeElementSource; import org.hibernate.metamodel.spi.source.MappedByAssociationSource; -import org.hibernate.metamodel.spi.source.MappingDefaults; -import org.hibernate.metamodel.spi.source.MappingException; import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeSource; import org.hibernate.metamodel.spi.source.MultiTenancySource; @@ -167,11 +163,9 @@ import org.hibernate.metamodel.spi.source.UniqueConstraintSource; import org.hibernate.metamodel.spi.source.VersionAttributeSource; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.service.ServiceRegistry; import org.hibernate.tuple.component.ComponentMetamodel; import org.hibernate.tuple.component.ComponentTuplizer; import org.hibernate.tuple.entity.EntityTuplizer; -import org.hibernate.type.EntityType; import org.hibernate.type.ForeignKeyDirection; import org.hibernate.type.Type; import org.jboss.logging.Logger; @@ -187,15 +181,13 @@ import org.jboss.logging.Logger; * @author Brett Meyer * @author Strong Liu */ -public class Binder { +public class Binder implements HelperContext { private static final CoreMessageLogger log = Logger.getMessageLogger( CoreMessageLogger.class, Binder.class.getName() ); - private final MetadataImplementor metadata; private final IdentifierGeneratorFactory identifierGeneratorFactory; - private final ObjectNameNormalizer nameNormalizer; // Entity hierarchies and source index need be available throughout the binding process private final Map entityHierarchiesByRootEntityName = @@ -204,14 +196,17 @@ public class Binder { // todo : apply org.hibernate.metamodel.MetadataSources.getExternalCacheRegionDefinitions() - private final LocalBindingContext bindingContext; + private final MetadataImplementor metadata; + + private final EntityHierarchyHelper.LocalBindingContextManager localBindingContextManager; - // helpers private final EntityHierarchyHelper entityHierarchyHelper; private final HibernateTypeHelper typeHelper; // todo: refactor helper and remove redundant methods in this class + private final RelationalIdentifierHelper relationalIdentifierHelper; private final TableHelper tableHelper; private final ForeignKeyHelper foreignKeyHelper; private final RelationalValueBindingHelper relationalValueBindingHelper; + private final NaturalIdUniqueKeyHelper naturalIdUniqueKeyHelper; private final StandardAssociationRelationalBindingResolverImpl standardAssociationRelationalBindingResolver; private final MappedByAssociationRelationalBindingResolverImpl mappedByAssociationRelationalBindingResolver; @@ -220,17 +215,18 @@ public class Binder { final IdentifierGeneratorFactory identifierGeneratorFactory) { this.metadata = metadata; this.identifierGeneratorFactory = identifierGeneratorFactory; - this.entityHierarchyHelper = new EntityHierarchyHelper( metadata ); - this.bindingContext = new BinderLocalBindingContextImpl( entityHierarchyHelper ); - this.typeHelper = new HibernateTypeHelper( bindingContext ); - this.tableHelper = new TableHelper( bindingContext ); - this.foreignKeyHelper = new ForeignKeyHelper( bindingContext ); - this.relationalValueBindingHelper = new RelationalValueBindingHelper( bindingContext ); - this.nameNormalizer = metadata.getObjectNameNormalizer(); + this.localBindingContextManager = new LocalBindingContextManagerImpl(); + this.entityHierarchyHelper = new EntityHierarchyHelper( localBindingContextManager ); + this.typeHelper = new HibernateTypeHelper( this ); + this.relationalIdentifierHelper = new RelationalIdentifierHelper( this ); + this.tableHelper = new TableHelper( this ); + this.foreignKeyHelper = new ForeignKeyHelper( this ); + this.relationalValueBindingHelper = new RelationalValueBindingHelper( this ); + this.naturalIdUniqueKeyHelper = new NaturalIdUniqueKeyHelper(); this.standardAssociationRelationalBindingResolver = - new StandardAssociationRelationalBindingResolverImpl( bindingContext ); + new StandardAssociationRelationalBindingResolverImpl( this ); this.mappedByAssociationRelationalBindingResolver = - new MappedByAssociationRelationalBindingResolverImpl( bindingContext ); + new MappedByAssociationRelationalBindingResolverImpl( this ); } @@ -247,16 +243,22 @@ public class Binder { LocalBindingContextExecutor executor = new LocalBindingContextExecutor() { @Override public void execute(LocalBindingContextExecutionContext bindingContextContext) { - sourceIndex.indexEntitySource( bindingContextContext.getRootEntitySource(), bindingContextContext.getEntitySource() ); + sourceIndex.indexEntitySource( + bindingContextContext.getRootEntitySource(), + bindingContextContext.getEntitySource() + ); createEntityBinding( bindingContextContext.getSuperEntityBinding(), - bindingContextContext.getEntitySource() + bindingContextContext.getEntitySource(), + bindingContextContext.getInheritanceType(), + bindingContextContext.getEntityMode() ); } private void resolveEntityLaziness( final EntityBinding entityBinding, - final EntitySource entitySource) { - if ( entityMode() == EntityMode.POJO ) { + final EntitySource entitySource, + final EntityMode entityMode) { + if ( entityMode == EntityMode.POJO ) { final String proxy = entitySource.getProxy(); if ( proxy == null ) { if ( entitySource.isLazy() ) { @@ -282,17 +284,16 @@ public class Binder { } private EntityBinding createEntityBinding( final EntityBinding superEntityBinding, - final EntitySource entitySource) { + final EntitySource entitySource, + final InheritanceType inheritanceType, + final EntityMode entityMode) { // Create binding final EntityBinding entityBinding = - entitySource instanceof RootEntitySource ? new EntityBinding( - inheritanceType(), - entityMode() - ) : new EntityBinding( - superEntityBinding - ); + entitySource instanceof RootEntitySource ? + new EntityBinding( inheritanceType, entityMode ) : + new EntityBinding( superEntityBinding ); // Create domain entity - final String entityClassName = entityMode() == EntityMode.POJO ? entitySource.getClassName() : null; + final String entityClassName = entityMode == EntityMode.POJO ? entitySource.getClassName() : null; LocalBindingContext bindingContext = bindingContext(); entityBinding.setEntity( new Entity( @@ -346,7 +347,7 @@ public class Binder { if ( entitySource.getSynchronizedTableNames() != null ) { entityBinding.addSynchronizedTableNames( entitySource.getSynchronizedTableNames() ); } - resolveEntityLaziness( entityBinding, entitySource ); + resolveEntityLaziness( entityBinding, entitySource, entityMode ); if ( entitySource.getFilterSources() != null ) { for ( FilterSource filterSource : entitySource.getFilterSources() ) { entityBinding.addFilterConfiguration( createFilterConfiguration( filterSource, entityBinding ) ); @@ -415,6 +416,41 @@ public class Binder { // TODO: when does this have to be done. } + @Override + public LocalBindingContext bindingContext() { + return localBindingContextManager.localBindingContext(); + } + + @Override + public HibernateTypeHelper typeHelper() { + return typeHelper; + } + + @Override + public RelationalIdentifierHelper relationalIdentifierHelper() { + return relationalIdentifierHelper; + } + + @Override + public TableHelper tableHelper() { + return tableHelper; + } + + @Override + public ForeignKeyHelper foreignKeyHelper() { + return foreignKeyHelper; + } + + @Override + public RelationalValueBindingHelper relationalValueBindingHelper() { + return relationalValueBindingHelper; + } + + @Override + public NaturalIdUniqueKeyHelper naturalIdUniqueKeyHelper() { + return naturalIdUniqueKeyHelper; + } + private LocalBindingContextExecutor bindIdentifierGeneratorExecutor() { return new LocalBindingContextExecutor() { @Override @@ -427,24 +463,16 @@ public class Binder { }; } - private InheritanceType inheritanceType() { - return entityHierarchyHelper.inheritanceType(); - } - - private EntityMode entityMode() { - return entityHierarchyHelper.entityMode(); - } - - private LocalBindingContext bindingContext() { - return bindingContext; - } - private void applyToAllEntityHierarchies(LocalBindingContextExecutor executor) { applyToAllEntityHierarchies( executor, executor ); } private void applyToAllEntityHierarchies(LocalBindingContextExecutor rootExecutor, LocalBindingContextExecutor subExecutor) { - entityHierarchyHelper.applyToAllEntityHierarchies( entityHierarchiesByRootEntityName.values(), rootExecutor, subExecutor ); + entityHierarchyHelper.applyToAllEntityHierarchies( + entityHierarchiesByRootEntityName.values(), + rootExecutor, + subExecutor + ); } @@ -863,7 +891,11 @@ public class Binder { for ( Iterator it = unresolvedEntityHierarchies.iterator(); it.hasNext(); ) { final EntityHierarchy entityHierarchy = it.next(); try { - entityHierarchyHelper.applyToEntityHierarchy( entityHierarchy, rootEntityCallback, subEntityCallback ); + entityHierarchyHelper.applyToEntityHierarchy( + entityHierarchy, + rootEntityCallback, + subEntityCallback + ); // succeeded, so the entityHierarchy is no longer unresolved. it.remove(); } @@ -1457,7 +1489,10 @@ public class Binder { properties.put( AvailableSettings.PREFER_POOLED_VALUES_LO, "false" ); } if ( !properties.contains( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER ) ) { - properties.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, nameNormalizer ); + properties.put( + PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, + metadata.getObjectNameNormalizer() + ); } final EntityIdentifier entityIdentifier = rootEntityBinding.getHierarchyDetails().getEntityIdentifier(); entityIdentifier.createIdentifierGenerator( identifierGeneratorFactory, properties ); @@ -2790,7 +2825,6 @@ public class Binder { final PluralAttributeSource attributeSource) { final PluralAttributeSource.Nature pluralAttributeSourceNature = attributeSource.getNature(); final PluralAttributeElementSource.Nature pluralElementSourceNature = attributeSource.getElementSource().getNature(); - final PluralAttributeElementBinding.Nature pluralElementBindingNature = attributeBinding.getPluralAttributeElementBinding().getNature(); //TODO what is this case? it would be really good to add a comment if ( pluralElementSourceNature == PluralAttributeElementSource.Nature.ONE_TO_MANY @@ -3066,82 +3100,8 @@ public class Binder { ); } - - public static interface DefaultNamingStrategy { String defaultName(NamingStrategy namingStrategy); } - - private static class BinderLocalBindingContextImpl implements LocalBindingContext { - private final EntityHierarchyHelper entityHierarchyHelper; - - BinderLocalBindingContextImpl(EntityHierarchyHelper entityHierarchyHelper) { - this.entityHierarchyHelper = entityHierarchyHelper; - } - - @Override - public Origin getOrigin() { - return bindingContext().getOrigin(); - } - - @Override - public MappingException makeMappingException(String message) { - return bindingContext().makeMappingException( message ); - } - - @Override - public MappingException makeMappingException(String message, Exception cause) { - return bindingContext().makeMappingException( message, cause ); - } - - @Override - public ServiceRegistry getServiceRegistry() { - return bindingContext().getServiceRegistry(); - } - - @Override - public NamingStrategy getNamingStrategy() { - return bindingContext().getNamingStrategy(); - } - - @Override - public MappingDefaults getMappingDefaults() { - return bindingContext().getMappingDefaults(); - } - - @Override - public MetadataImplementor getMetadataImplementor() { - return bindingContext().getMetadataImplementor(); - } - - @Override - public Class locateClassByName(String name) { - return bindingContext().locateClassByName( name ); - } - - @Override - public org.hibernate.metamodel.spi.domain.Type makeJavaType(String className) { - return bindingContext().makeJavaType( className ); - } - - @Override - public boolean isGloballyQuotedIdentifiers() { - return bindingContext().isGloballyQuotedIdentifiers(); - } - - @Override - public ValueHolder> makeClassReference(String className) { - return bindingContext().makeClassReference( className ); - } - - @Override - public String qualifyClassName(String name) { - return bindingContext().qualifyClassName( name ); - } - - private LocalBindingContext bindingContext() { - return entityHierarchyHelper.bindingContext(); - } - } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityHierarchyHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityHierarchyHelper.java index 8b0a9cb4f2..d076813d49 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityHierarchyHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityHierarchyHelper.java @@ -23,10 +23,7 @@ */ package org.hibernate.metamodel.internal; -import java.util.LinkedList; - import org.hibernate.EntityMode; -import org.hibernate.metamodel.spi.MetadataImplementor; import org.hibernate.metamodel.spi.binding.EntityBinding; import org.hibernate.metamodel.spi.binding.InheritanceType; import org.hibernate.metamodel.spi.source.EntityHierarchy; @@ -39,19 +36,32 @@ import org.hibernate.metamodel.spi.source.SubclassEntitySource; * @author Gail Badner */ public class EntityHierarchyHelper { + public interface LocalBindingContextExecutor { void execute(LocalBindingContextExecutionContext bindingContextContext); } - private MetadataImplementor metadata; + public interface LocalBindingContextExecutionContext { + RootEntitySource getRootEntitySource(); + EntitySource getEntitySource(); + EntityBinding getEntityBinding(); + EntityBinding getSuperEntityBinding(); + InheritanceType getInheritanceType(); + EntityMode getEntityMode(); + } - // the inheritanceTypes and entityModes correspond with bindingContexts - private final LinkedList bindingContexts = new LinkedList(); - private final LinkedList inheritanceTypes = new LinkedList(); - private final LinkedList entityModes = new LinkedList(); + public static interface LocalBindingContextManager extends LocalBindingContextExecutionContext { + LocalBindingContext localBindingContext(); + void cleanupLocalBindingContexts(); + void setupLocalBindingContexts(final EntityHierarchy entityHierarchy); + void pushSubEntitySource(EntitySource entitySource); + void popSubEntitySource(); + } - EntityHierarchyHelper(final MetadataImplementor metadata) { - this.metadata = metadata; + private LocalBindingContextManager localBindingContextManager; + + EntityHierarchyHelper(final LocalBindingContextManager localBindingContextManager) { + this.localBindingContextManager = localBindingContextManager; } /** @@ -79,127 +89,33 @@ public class EntityHierarchyHelper { final EntityHierarchy entityHierarchy, final LocalBindingContextExecutor rootEntityExecutor, final LocalBindingContextExecutor subEntityExecutor) { - bindingContexts.clear(); - inheritanceTypes.clear(); - entityModes.clear(); - final RootEntitySource rootEntitySource = entityHierarchy.getRootEntitySource(); - setupBindingContext( entityHierarchy, rootEntitySource ); + localBindingContextManager.cleanupLocalBindingContexts(); + localBindingContextManager.setupLocalBindingContexts( entityHierarchy ); try { - LocalBindingContextExecutionContext executionContext = - new LocalBindingContextExecutionContextImpl( rootEntitySource, null ); - rootEntityExecutor.execute( executionContext ); - if ( inheritanceTypes.peek() != InheritanceType.NO_INHERITANCE ) { - applyToSubEntities( - executionContext.getEntityBinding(), - rootEntitySource, - rootEntitySource, - subEntityExecutor ); + rootEntityExecutor.execute( localBindingContextManager ); + if ( entityHierarchy.getHierarchyInheritanceType() != InheritanceType.NO_INHERITANCE ) { + applyToSubEntities( subEntityExecutor ); } } finally { - cleanupBindingContext(); + localBindingContextManager.cleanupLocalBindingContexts(); } } - private void cleanupBindingContext() { - bindingContexts.pop(); - inheritanceTypes.pop(); - entityModes.pop(); - } - - public LocalBindingContext bindingContext() { - return bindingContexts.peek(); - } - - public InheritanceType inheritanceType() { - return inheritanceTypes.peek(); - } - - public EntityMode entityMode() { - return entityModes.peek(); - } - - private void setupBindingContext( - final EntityHierarchy entityHierarchy, - final RootEntitySource rootEntitySource) { - // Save inheritance type and entity mode that will apply to entire hierarchy - inheritanceTypes.push( entityHierarchy.getHierarchyInheritanceType() ); - entityModes.push( rootEntitySource.getEntityMode() ); - bindingContexts.push( rootEntitySource.getLocalBindingContext() ); - } - - private void applyToSubEntities( - final EntityBinding entityBinding, - final RootEntitySource rootEntitySource, - final EntitySource entitySource, - final LocalBindingContextExecutor subEntityExecutor) { - for ( final SubclassEntitySource subEntitySource : entitySource.subclassEntitySources() ) { - applyToSubEntity( entityBinding, rootEntitySource, subEntitySource, subEntityExecutor ); + private void applyToSubEntities(final LocalBindingContextExecutor subEntityExecutor) { + for ( final SubclassEntitySource subEntitySource : localBindingContextManager.getEntitySource().subclassEntitySources() ) { + applyToSubEntity( subEntitySource, subEntityExecutor ); } } - private void applyToSubEntity( - final EntityBinding superEntityBinding, - final RootEntitySource rootEntitySource, - final EntitySource entitySource, - final LocalBindingContextExecutor subEntityExecutor) { - final LocalBindingContext bindingContext = entitySource.getLocalBindingContext(); - bindingContexts.push( bindingContext ); + private void applyToSubEntity(final EntitySource entitySource, final LocalBindingContextExecutor subEntityExecutor) { + localBindingContextManager.pushSubEntitySource( entitySource ); try { - LocalBindingContextExecutionContext executionContext = - new LocalBindingContextExecutionContextImpl( rootEntitySource, entitySource, superEntityBinding ); - subEntityExecutor.execute( executionContext ); - applyToSubEntities( executionContext.getEntityBinding(), rootEntitySource, entitySource, subEntityExecutor ); + subEntityExecutor.execute( localBindingContextManager ); + applyToSubEntities( subEntityExecutor ); } finally { - bindingContexts.pop(); - } - } - - public interface LocalBindingContextExecutionContext { - RootEntitySource getRootEntitySource(); - EntitySource getEntitySource(); - EntityBinding getEntityBinding(); - EntityBinding getSuperEntityBinding(); - } - - private class LocalBindingContextExecutionContextImpl implements LocalBindingContextExecutionContext { - private final RootEntitySource rootEntitySource; - private final EntitySource entitySource; - private final EntityBinding superEntityBinding; - - private LocalBindingContextExecutionContextImpl( - RootEntitySource rootEntitySource, - EntityBinding superEntityBinding) { - this.rootEntitySource = rootEntitySource; - this.entitySource = rootEntitySource; - this.superEntityBinding = superEntityBinding; - } - - private LocalBindingContextExecutionContextImpl( - RootEntitySource rootEntitySource, - EntitySource entitySource, - EntityBinding superEntityBinding) { - this.rootEntitySource = rootEntitySource; - this.entitySource = entitySource; - this.superEntityBinding = superEntityBinding; - } - - @Override - public RootEntitySource getRootEntitySource() { - return rootEntitySource; - } - @Override - public EntitySource getEntitySource() { - return entitySource; - } - @Override - public EntityBinding getEntityBinding() { - return metadata.getEntityBinding( entitySource.getEntityName() ); - } - @Override - public EntityBinding getSuperEntityBinding() { - return superEntityBinding; + localBindingContextManager.popSubEntitySource(); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ForeignKeyHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ForeignKeyHelper.java index 682ca26cc1..9b79a73632 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ForeignKeyHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/ForeignKeyHelper.java @@ -55,12 +55,10 @@ public class ForeignKeyHelper { ForeignKeyHelper.class.getName() ); - private final LocalBindingContext bindingContext; - private final RelationalIdentifierHelper relationalIdentifierHelper; + private final HelperContext helperContext; - public ForeignKeyHelper(LocalBindingContext bindingContext) { - this.bindingContext = bindingContext; - this.relationalIdentifierHelper = new RelationalIdentifierHelper( bindingContext ); + public ForeignKeyHelper(HelperContext helperContext) { + this.helperContext = helperContext; } public List determineForeignKeyTargetColumns( @@ -82,7 +80,7 @@ public class ForeignKeyHelper { final ForeignKeyContributingSource.JoinColumnResolutionContext resolutionContext = new JoinColumnResolutionContextImpl( entityBinding ); for ( Value relationalValue : fkColumnResolutionDelegate.getJoinColumns( resolutionContext ) ) { if ( !Column.class.isInstance( relationalValue ) ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( "Foreign keys can currently only name columns, not formulas" ); } @@ -130,7 +128,7 @@ public class ForeignKeyHelper { if ( referencedAttributeBinding == null ) { if ( explicitName != null ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "No attribute binding found with name: %s.%s", referencedEntityBinding.getEntityName(), @@ -146,7 +144,7 @@ public class ForeignKeyHelper { } if ( !referencedAttributeBinding.getAttribute().isSingular() ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Foreign key references a non-singular attribute [%s]", referencedAttributeBinding.getAttribute().getName() @@ -167,7 +165,7 @@ public class ForeignKeyHelper { foreignKeyName = ForeignKey.generateName( sourceTable, targetTable, sourceColumns, targetColumns ); } else { - foreignKeyName = relationalIdentifierHelper.quotedIdentifier( explicitForeignKeyName ); + foreignKeyName = helperContext.relationalIdentifierHelper().quotedIdentifier( explicitForeignKeyName ); } ForeignKey foreignKey = locateAndBindForeignKeyByName( foreignKeyName, sourceTable, sourceColumns, targetTable, targetColumns ); @@ -227,7 +225,7 @@ public class ForeignKeyHelper { final TableSpecification targetTable, final List targetColumns) { if ( sourceColumns.size() != targetColumns.size() ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Non-matching number columns in foreign key source columns [%s : %s] and target columns [%s : %s]", sourceTable.getLogicalName().getText(), @@ -254,7 +252,7 @@ public class ForeignKeyHelper { ForeignKey foreignKey = sourceTable.locateForeignKey( foreignKeyName ); if ( foreignKey != null ) { if ( !targetTable.equals( foreignKey.getTargetTable() ) ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Unexpected target table defined for foreign key \"%s\"; expected \"%s\"; found \"%s\"", foreignKeyName, @@ -273,7 +271,7 @@ public class ForeignKeyHelper { // Make sure they are the same columns. if ( !foreignKey.getSourceColumns().equals( sourceColumns ) || !foreignKey.getTargetColumns().equals( targetColumns ) ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Attempt to bind exisitng foreign key \"%s\" with different columns.", foreignKeyName @@ -285,7 +283,11 @@ public class ForeignKeyHelper { return foreignKey; } - public class JoinColumnResolutionContextImpl implements ForeignKeyContributingSource.JoinColumnResolutionContext { + private LocalBindingContext bindingContext() { + return helperContext.bindingContext(); + } + + private class JoinColumnResolutionContextImpl implements ForeignKeyContributingSource.JoinColumnResolutionContext { private final EntityBinding referencedEntityBinding; @@ -299,7 +301,7 @@ public class ForeignKeyHelper { String logicalTableName, String logicalSchemaName, String logicalCatalogName) { - if ( bindingContext.isGloballyQuotedIdentifiers() && !org.hibernate + if ( bindingContext().isGloballyQuotedIdentifiers() && !org.hibernate .internal .util .StringHelper @@ -313,7 +315,7 @@ public class ForeignKeyHelper { @Override public TableSpecification resolveTable(String logicalTableName, String logicalSchemaName, String logicalCatalogName) { - Identifier tableIdentifier = relationalIdentifierHelper.createIdentifier( logicalTableName ); + Identifier tableIdentifier = helperContext.relationalIdentifierHelper().createIdentifier( logicalTableName ); if ( tableIdentifier == null ) { tableIdentifier = referencedEntityBinding.getPrimaryTable().getLogicalName(); } @@ -325,7 +327,7 @@ public class ForeignKeyHelper { Identifier schemaName = org.hibernate.internal.util.StringHelper.isNotEmpty( logicalCatalogName ) ? Identifier.toIdentifier( logicalSchemaName ) : referencedEntityBinding.getPrimaryTable().getSchema().getName().getSchema(); - Schema schema = bindingContext.getMetadataImplementor().getDatabase().getSchema( catalogName, schemaName ); + Schema schema = bindingContext().getMetadataImplementor().getDatabase().getSchema( catalogName, schemaName ); return schema.locateTable( tableIdentifier ); } @@ -364,7 +366,7 @@ public class ForeignKeyHelper { final AttributeBinding referencedAttributeBinding = referencedEntityBinding.locateAttributeBindingByPath( attributeName, true ); if ( referencedAttributeBinding == null ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Could not resolve named referenced property [%s] against entity [%s]", attributeName, @@ -373,7 +375,7 @@ public class ForeignKeyHelper { ); } if ( !referencedAttributeBinding.getAttribute().isSingular() ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Referenced property [%s] against entity [%s] is a plural attribute; it must be a singular attribute.", attributeName, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HelperContext.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HelperContext.java new file mode 100644 index 0000000000..9c63fcb591 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/HelperContext.java @@ -0,0 +1,39 @@ +/* + * 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.metamodel.internal; + +import org.hibernate.metamodel.spi.source.LocalBindingContext; + +/** + * @author Gail Badner + */ +public interface HelperContext { + HibernateTypeHelper typeHelper(); + RelationalIdentifierHelper relationalIdentifierHelper(); + TableHelper tableHelper(); + ForeignKeyHelper foreignKeyHelper(); + RelationalValueBindingHelper relationalValueBindingHelper(); + NaturalIdUniqueKeyHelper naturalIdUniqueKeyHelper(); + LocalBindingContext bindingContext(); +} 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 1e6878b22f..52d84f5941 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 @@ -263,11 +263,11 @@ class HibernateTypeHelper { } //-------------------------------------------------------------------------------- - private final LocalBindingContext bindingContext; + private final HelperContext helperContext; //package scope methods - HibernateTypeHelper(LocalBindingContext bindingContext) { - this.bindingContext = bindingContext; + HibernateTypeHelper(HelperContext helperContext) { + this.helperContext = helperContext; } /** @@ -314,7 +314,7 @@ class HibernateTypeHelper { ? EntityType.class.cast( resolvedHibernateType ).getIdentifierOrUniqueKeyType( metadata() ) : resolvedHibernateType; if ( !CompositeType.class.isInstance( resolvedRelationalType ) ) { - throw bindingContext + throw bindingContext() .makeMappingException( "Column number mismatch" ); // todo refine the exception message } Type[] subTypes = CompositeType.class.cast( resolvedRelationalType ).getSubtypes(); @@ -591,7 +591,7 @@ class HibernateTypeHelper { final String defaultJavaTypeName) { if ( explicitTypeName == null ) { if ( defaultJavaTypeName != null && hibernateTypeDescriptor.getJavaTypeName() != null ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Attempt to re-initialize (non-explicit) Java type name; current=%s new=%s", hibernateTypeDescriptor.getJavaTypeName(), @@ -605,7 +605,7 @@ class HibernateTypeHelper { // Check if user-specified name is of a User-Defined Type (UDT) final TypeDefinition typeDef = metadata().getTypeDefinition( explicitTypeName ); if ( hibernateTypeDescriptor.getExplicitTypeName() != null ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Attempt to re-initialize explicity-mapped Java type name; current=%s new=%s", hibernateTypeDescriptor.getExplicitTypeName(), @@ -632,7 +632,7 @@ class HibernateTypeHelper { final SingularAttributeBinding attributeBinding, final Type resolvedHibernateType) { if ( resolvedHibernateType == null ) { - throw bindingContext.makeMappingException( "Resolved hibernate type can't be null" ); + throw bindingContext().makeMappingException( "Resolved hibernate type can't be null" ); } final HibernateTypeDescriptor hibernateTypeDescriptor = attributeBinding.getHibernateTypeDescriptor(); if ( hibernateTypeDescriptor.getResolvedTypeMapping() == null ) { @@ -701,12 +701,16 @@ class HibernateTypeHelper { } } + private LocalBindingContext bindingContext() { + return helperContext.bindingContext(); + } + private MetadataImplementor metadata() { - return bindingContext.getMetadataImplementor(); + return bindingContext().getMetadataImplementor(); } private org.hibernate.metamodel.spi.domain.Type makeJavaType(String name) { - return bindingContext.makeJavaType( name ); + return bindingContext().makeJavaType( name ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/LocalBindingContextManagerImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/LocalBindingContextManagerImpl.java new file mode 100644 index 0000000000..636c364e7e --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/LocalBindingContextManagerImpl.java @@ -0,0 +1,176 @@ +/* + * 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.metamodel.internal; + +import java.util.LinkedList; + +import org.hibernate.EntityMode; +import org.hibernate.metamodel.spi.MetadataImplementor; +import org.hibernate.metamodel.spi.binding.EntityBinding; +import org.hibernate.metamodel.spi.binding.InheritanceType; +import org.hibernate.metamodel.spi.source.EntityHierarchy; +import org.hibernate.metamodel.spi.source.EntitySource; +import org.hibernate.metamodel.spi.source.LocalBindingContext; +import org.hibernate.metamodel.spi.source.RootEntitySource; + +/** + * @author Gail Badner + */ +public class LocalBindingContextManagerImpl implements EntityHierarchyHelper.LocalBindingContextManager { + private final LocalBindingContextsImpl localBindingContexts = new LocalBindingContextsImpl(); + + @Override + public void cleanupLocalBindingContexts() { + localBindingContexts.cleanup(); + } + + @Override + public void setupLocalBindingContexts(final EntityHierarchy entityHierarchy) { + localBindingContexts.setup( entityHierarchy ); + } + + @Override + public void pushSubEntitySource(EntitySource entitySource) { + localBindingContexts.pushEntitySource( entitySource ); + } + + @Override + public void popSubEntitySource() { + localBindingContexts.popEntitySource(); + } + + @Override + public EntityMode getEntityMode() { + return entityHierarchy().getRootEntitySource().getEntityMode(); + } + + @Override + public InheritanceType getInheritanceType() { + return entityHierarchy().getHierarchyInheritanceType(); + } + + @Override + public RootEntitySource getRootEntitySource() { + return entityHierarchy().getRootEntitySource(); + } + + @Override + public EntitySource getEntitySource() { + if ( localBindingContexts.isEmpty() ) { + throw new IllegalStateException( "No LocalBindingContext defined." ); + } + return localBindingContexts.entitySource(); + } + + @Override + public EntityBinding getEntityBinding() { + return getMetadataImplementor().getEntityBinding( getEntitySource().getEntityName() ); + } + + @Override + public EntityBinding getSuperEntityBinding() { + final EntitySource superEntitySource = getSuperEntitySource(); + return superEntitySource == null ? + null : + getMetadataImplementor().getEntityBinding( superEntitySource.getEntityName() ); + } + + @Override + public LocalBindingContext localBindingContext() { + return getEntitySource().getLocalBindingContext(); + } + + private MetadataImplementor getMetadataImplementor() { + return localBindingContext().getMetadataImplementor(); + } + + private EntitySource getSuperEntitySource() { + if ( localBindingContexts.isEmpty() ) { + throw new IllegalStateException( "No LocalBindingContext defined." ); + } + return localBindingContexts.superEntitySource(); + } + + private EntityHierarchy entityHierarchy() { + if ( localBindingContexts.isEmpty() ) { + throw new IllegalStateException( "No LocalBindingContext defined." ); + } + return localBindingContexts.entityHierarchy(); + } + + // Each EntitySource contains its LocalBindingContext. + private class LocalBindingContextsImpl { + private EntityHierarchy entityHierarchy; + private final LinkedList entitySources = new LinkedList( ); + + private boolean isEmpty() { + return entityHierarchy == null; + } + + private void setup(final EntityHierarchy entityHierarchy) { + // Inheritance type and entity mode applies to entire hierarchy + if ( entityHierarchy == null || entityHierarchy.getRootEntitySource() == null ) { + throw new IllegalArgumentException( + "entityHierarchy and entityHierarchy.getRootEntitySource() must be non-null." + ); + } + if ( this.entityHierarchy != null ) { + throw new IllegalStateException( "Attempt to initialize entityHierarchy when it is already initialized." ); + } + this.entityHierarchy = entityHierarchy; + this.entitySources.push( entityHierarchy.getRootEntitySource() ); + } + + private void cleanup() { + entityHierarchy = null; + entitySources.clear(); + } + + private void pushEntitySource(EntitySource entitySource) { + entitySources.push( entitySource ); + } + + private void popEntitySource() { + entitySources.pop(); + } + + private EntityHierarchy entityHierarchy() { + return entityHierarchy; + } + + private EntitySource entitySource() { + return entitySources.peek(); + } + + private EntitySource superEntitySource() { + if ( entitySources.size() == 1 ) { + return null; + } + final EntitySource currentEntitySource = entitySources.pop(); + final EntitySource superEntitySource = entitySources.peek(); + entitySources.push( currentEntitySource ); + return superEntitySource; + } + } +} \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalIdentifierHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalIdentifierHelper.java index b96a66a48c..789bf748d3 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalIdentifierHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalIdentifierHelper.java @@ -33,19 +33,16 @@ import org.hibernate.metamodel.spi.source.LocalBindingContext; */ public class RelationalIdentifierHelper { - private final LocalBindingContext bindingContext; + private final HelperContext helperContext; - RelationalIdentifierHelper(LocalBindingContext bindingContext) { - this.bindingContext = bindingContext; + RelationalIdentifierHelper(HelperContext helperContext) { + this.helperContext = helperContext; } public String normalizeDatabaseIdentifier( final String explicitName, - ObjectNameNormalizer.NamingStrategyHelper helper) { - return bindingContext - .getMetadataImplementor() - .getObjectNameNormalizer() - .normalizeDatabaseIdentifier( explicitName, helper ); + final ObjectNameNormalizer.NamingStrategyHelper helper) { + return getObjectNameNormalizer().normalizeDatabaseIdentifier( explicitName, helper ); } public Identifier createIdentifier(final String name){ @@ -59,7 +56,10 @@ public class RelationalIdentifierHelper { } public String quotedIdentifier(final String name) { - return bindingContext.getMetadataImplementor().getObjectNameNormalizer().normalizeIdentifierQuoting( name ); + return getObjectNameNormalizer().normalizeIdentifierQuoting( name ); } + private ObjectNameNormalizer getObjectNameNormalizer() { + return helperContext.bindingContext().getMetadataImplementor().getObjectNameNormalizer(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalValueBindingHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalValueBindingHelper.java index 8a45cb123d..fa89b674e1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalValueBindingHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/RelationalValueBindingHelper.java @@ -40,7 +40,6 @@ import org.hibernate.metamodel.spi.relational.TableSpecification; import org.hibernate.metamodel.spi.relational.Value; import org.hibernate.metamodel.spi.source.ColumnSource; import org.hibernate.metamodel.spi.source.DerivedValueSource; -import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.RelationalValueSource; import org.hibernate.metamodel.spi.source.RelationalValueSourceContainer; import org.hibernate.metamodel.spi.source.SingularAttributeSource; @@ -49,23 +48,21 @@ import org.hibernate.metamodel.spi.source.SingularAttributeSource; * @author Gail Badner */ public class RelationalValueBindingHelper { - private final TableHelper tableHelper; - private final NaturalIdUniqueKeyHelper uniqueKeyHelper; - public RelationalValueBindingHelper(LocalBindingContext bindingContext) { - this.tableHelper = new TableHelper( bindingContext ); - this.uniqueKeyHelper = new NaturalIdUniqueKeyHelper(); + private final HelperContext helperContext; + + public RelationalValueBindingHelper(HelperContext helperContext) { + this.helperContext = helperContext; } public boolean hasDerivedValue(List relationalValueBindings) { for ( RelationalValueBinding relationalValueBinding : relationalValueBindings ) { - if (relationalValueBinding.isDerived() ) { - return true; + if (relationalValueBinding.isDerived() ) { + return true; + } } + return false; } - return false; -} - public List createRelationalValueBindings( final AttributeBindingContainer attributeBindingContainer, @@ -105,14 +102,14 @@ public class RelationalValueBindingHelper { if ( valueSourceContainer.relationalValueSources().isEmpty() ) { for ( Binder.DefaultNamingStrategy defaultNameStrategy : defaultNameStrategies ) { - final Column column = tableHelper.locateOrCreateColumn( + final Column column = helperContext.tableHelper().locateOrCreateColumn( defaultTable, null, new DefaultColumnNamingStrategyHelper( defaultNameStrategy ) ); column.setNullable( !reallyForceNonNullable && valueSourceContainer.areValuesNullableByDefault() ); if ( isNaturalId ) { - uniqueKeyHelper.addUniqueConstraintForNaturalIdColumn( defaultTable, column ); + helperContext.naturalIdUniqueKeyHelper().addUniqueConstraintForNaturalIdColumn( defaultTable, column ); } valueBindings.add( new RelationalValueBinding( @@ -141,7 +138,7 @@ public class RelationalValueBindingHelper { defaultNameStrategies.get( i ) : null ); - Column column = tableHelper.locateOrCreateColumn( + Column column = helperContext.tableHelper().locateOrCreateColumn( table, columnSource, defaultColumnNamingStrategyHelper, @@ -149,7 +146,7 @@ public class RelationalValueBindingHelper { valueSourceContainer.areValuesNullableByDefault() ); if ( isNaturalId ) { - uniqueKeyHelper.addUniqueConstraintForNaturalIdColumn( table, column ); + helperContext.naturalIdUniqueKeyHelper().addUniqueConstraintForNaturalIdColumn( table, column ); } final boolean isIncludedInInsert = TruthValue.toBoolean( diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java index 1ba7899daa..056e58ad2d 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/TableHelper.java @@ -28,7 +28,6 @@ import org.jboss.logging.Logger; import org.hibernate.TruthValue; import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.metamodel.spi.binding.AttributeBindingContainer; import org.hibernate.metamodel.spi.relational.Column; import org.hibernate.metamodel.spi.relational.Identifier; import org.hibernate.metamodel.spi.relational.Schema; @@ -39,11 +38,9 @@ import org.hibernate.metamodel.spi.source.ColumnSource; import org.hibernate.metamodel.spi.source.InLineViewSource; import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.MappingDefaults; -import org.hibernate.metamodel.spi.source.SingularAttributeSource; import org.hibernate.metamodel.spi.source.SizeSource; import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.TableSpecificationSource; -import org.hibernate.metamodel.spi.source.ToOneAttributeSource; /** * @author Gail Badner @@ -54,12 +51,10 @@ public class TableHelper { TableHelper.class.getName() ); - private final LocalBindingContext bindingContext; - private final RelationalIdentifierHelper relationalIdentifierHelper; + private final HelperContext helperContext; - public TableHelper(LocalBindingContext bindingContext) { - this.bindingContext = bindingContext; - this.relationalIdentifierHelper = new RelationalIdentifierHelper( bindingContext ); + public TableHelper(HelperContext helperContext) { + this.helperContext = helperContext; } public TableSpecification createTable( @@ -73,7 +68,9 @@ public class TableHelper { final ObjectNameNormalizer.NamingStrategyHelper namingStrategyHelper, final Table includedTable) { if ( tableSpecSource == null && namingStrategyHelper == null ) { - throw bindingContext.makeMappingException( "An explicit name must be specified for the table" ); + throw bindingContext().makeMappingException( + "An explicit name must be specified for the table" + ); } final boolean isTableSourceNull = tableSpecSource == null; final Schema schema = resolveSchema( tableSpecSource ); @@ -83,7 +80,7 @@ public class TableHelper { String explicitName = isTableSourceNull ? null : TableSource.class.cast( tableSpecSource ).getExplicitTableName(); String tableName = normalizeDatabaseIdentifier( explicitName, namingStrategyHelper ); String logicTableName = TableNamingStrategyHelper.class.cast( namingStrategyHelper ).getLogicalName( - bindingContext.getNamingStrategy() + bindingContext().getNamingStrategy() ); tableSpec = createTableSpecification( schema, tableName, logicTableName, includedTable ); } @@ -99,7 +96,7 @@ public class TableHelper { public Schema resolveSchema(final TableSpecificationSource tableSpecSource) { final boolean tableSourceNull = tableSpecSource == null; - final MappingDefaults mappingDefaults = bindingContext.getMappingDefaults(); + final MappingDefaults mappingDefaults = bindingContext().getMappingDefaults(); final String explicitCatalogName = tableSourceNull ? null : tableSpecSource.getExplicitCatalogName(); final String explicitSchemaName = tableSourceNull ? null : tableSpecSource.getExplicitSchemaName(); final Schema.Name schemaName = @@ -107,7 +104,7 @@ public class TableHelper { createIdentifier( explicitCatalogName, mappingDefaults.getCatalogName() ), createIdentifier( explicitSchemaName, mappingDefaults.getSchemaName() ) ); - return bindingContext.getMetadataImplementor().getDatabase().locateSchema( schemaName ); + return bindingContext().getMetadataImplementor().getDatabase().locateSchema( schemaName ); } public TableSpecification createTableSpecification( @@ -136,7 +133,7 @@ public class TableHelper { final String columnName, final ObjectNameNormalizer.NamingStrategyHelper namingStrategyHelper) { if ( columnName == null && namingStrategyHelper == null ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( "Cannot resolve name for column because no name was specified and namingStrategyHelper is null." ); } @@ -210,14 +207,18 @@ public class TableHelper { } private Identifier createIdentifier(String name) { - return relationalIdentifierHelper.createIdentifier( name ); + return helperContext.relationalIdentifierHelper().createIdentifier( name ); } private Identifier createIdentifier(String name, String defaultName) { - return relationalIdentifierHelper.createIdentifier( name, defaultName ); + return helperContext.relationalIdentifierHelper().createIdentifier( name, defaultName ); } private String normalizeDatabaseIdentifier(String explicitName, ObjectNameNormalizer.NamingStrategyHelper namingStrategyHelper) { - return relationalIdentifierHelper.normalizeDatabaseIdentifier( explicitName, namingStrategyHelper ); + return helperContext.relationalIdentifierHelper().normalizeDatabaseIdentifier( explicitName, namingStrategyHelper ); + } + + private LocalBindingContext bindingContext() { + return helperContext.bindingContext(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/MappedByAssociationRelationalBindingResolverImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/MappedByAssociationRelationalBindingResolverImpl.java index 6edf407781..1e10ccb64a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/MappedByAssociationRelationalBindingResolverImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/MappedByAssociationRelationalBindingResolverImpl.java @@ -29,6 +29,7 @@ import java.util.List; import org.hibernate.AssertionFailure; import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.metamodel.internal.ForeignKeyHelper; +import org.hibernate.metamodel.internal.HelperContext; import org.hibernate.metamodel.internal.RelationalValueBindingHelper; import org.hibernate.metamodel.spi.binding.AttributeBinding; import org.hibernate.metamodel.spi.binding.AttributeBindingContainer; @@ -55,14 +56,10 @@ import org.hibernate.type.ForeignKeyDirection; + * @author Gail Badner + */ public class MappedByAssociationRelationalBindingResolverImpl implements AssociationRelationalBindingResolver { - private final LocalBindingContext bindingContext; - private final ForeignKeyHelper foreignKeyHelper; - private final RelationalValueBindingHelper relationalValueBindingHelper; + private final HelperContext helperContext; - public MappedByAssociationRelationalBindingResolverImpl(LocalBindingContext bindingContext) { - this.bindingContext = bindingContext; - this.foreignKeyHelper = new ForeignKeyHelper( bindingContext ); - this.relationalValueBindingHelper = new RelationalValueBindingHelper( bindingContext ); + public MappedByAssociationRelationalBindingResolverImpl(HelperContext helperContext) { + this.helperContext = helperContext; } @Override @@ -93,15 +90,15 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa if ( attributeSource.getForeignKeyDirection() == ForeignKeyDirection.TO_PARENT ) { throw new AssertionFailure( "Cannot create a foreign key for one-to-one with foreign key direction going to the parent." ); } - final List targetColumns = foreignKeyHelper.determineForeignKeyTargetColumns( + final List targetColumns = foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, attributeSource ); - final TableSpecification targetTable = foreignKeyHelper.determineForeignKeyTargetTable( + final TableSpecification targetTable = foreignKeyHelper().determineForeignKeyTargetTable( referencedEntityBinding, attributeSource ); - return foreignKeyHelper.locateOrCreateForeignKey( + return foreignKeyHelper().locateOrCreateForeignKey( attributeSource.getExplicitForeignKeyName(), sourceTable, sourceColumns, @@ -143,7 +140,7 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa getMappedByAssociationSource( attributeSource ), referencedEntityBinding ); - return relationalValueBindingHelper.bindInverseRelationalValueBindings( + return relationalValueBindingHelper().bindInverseRelationalValueBindings( ownerSecondaryTable.getForeignKeyReference().getSourceTable(), ownerSecondaryTable.getForeignKeyReference().getSourceColumns() ); @@ -180,14 +177,14 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa // for the secondary table. final SecondaryTable ownerSecondaryTable = referencedEntityBinding.getSecondaryTables().get( collectionTable.getLogicalName() ); - relationalValueBindings = relationalValueBindingHelper.bindInverseRelationalValueBindings( + relationalValueBindings = relationalValueBindingHelper().bindInverseRelationalValueBindings( collectionTable, ownerSecondaryTable.getForeignKeyReference().getSourceColumns() ); } else { final PluralAttributeBinding ownerPluralAttributeBinding = (PluralAttributeBinding) ownerAttributeBinding; - relationalValueBindings = relationalValueBindingHelper.bindInverseRelationalValueBindings( + relationalValueBindings = relationalValueBindingHelper().bindInverseRelationalValueBindings( collectionTable, ownerPluralAttributeBinding.getPluralAttributeKeyBinding().getValues() ); @@ -307,6 +304,10 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa return referencedAttributeBinding; } + private LocalBindingContext bindingContext() { + return helperContext.bindingContext(); + } + private MappedByAssociationSource getMappedByAssociationSource(AssociationSource associationSource) { if ( !associationSource.isMappedBy() || !MappedByAssociationSource.class.isInstance( associationSource ) ) { throw new AssertionFailure( "Expected a MappedByAssociationSource." ); @@ -315,7 +316,7 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa } private AttributeBinding getOwnerAttributeBinding(MappedByAssociationSource associationSource) { - final EntityBinding referencedEntityBinding = bindingContext.getMetadataImplementor().getEntityBinding( + final EntityBinding referencedEntityBinding = bindingContext().getMetadataImplementor().getEntityBinding( associationSource.getReferencedEntityName() ); final AttributeBinding ownerAttributeBinding = referencedEntityBinding.locateAttributeBindingByPath( @@ -323,7 +324,7 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa true ); if ( ownerAttributeBinding == null ) { - throw bindingContext.makeMappingException( + throw bindingContext().makeMappingException( String.format( "Attribute not found: [%s.%s]", referencedEntityBinding.getEntityName(), @@ -347,4 +348,12 @@ public class MappedByAssociationRelationalBindingResolverImpl implements Associa } return referencedEntityBinding.getSecondaryTables().get( table.getLogicalName() ); } + + private ForeignKeyHelper foreignKeyHelper() { + return helperContext.foreignKeyHelper(); + } + + private RelationalValueBindingHelper relationalValueBindingHelper() { + return helperContext.relationalValueBindingHelper(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/StandardAssociationRelationalBindingResolverImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/StandardAssociationRelationalBindingResolverImpl.java index ce7886932c..77ddd77fc5 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/StandardAssociationRelationalBindingResolverImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/resolver/StandardAssociationRelationalBindingResolverImpl.java @@ -32,6 +32,7 @@ import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.metamodel.internal.Binder; import org.hibernate.metamodel.internal.ForeignKeyHelper; +import org.hibernate.metamodel.internal.HelperContext; import org.hibernate.metamodel.internal.ManyToManyCollectionTableNamingStrategyHelper; import org.hibernate.metamodel.internal.RelationalValueBindingHelper; import org.hibernate.metamodel.internal.TableHelper; @@ -45,7 +46,6 @@ import org.hibernate.metamodel.spi.relational.TableSpecification; import org.hibernate.metamodel.spi.relational.Value; import org.hibernate.metamodel.spi.source.AssociationSource; import org.hibernate.metamodel.spi.source.ForeignKeyContributingSource; -import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.ManyToManyPluralAttributeElementSource; import org.hibernate.metamodel.spi.source.PluralAttributeElementSource; import org.hibernate.metamodel.spi.source.PluralAttributeKeySource; @@ -60,14 +60,10 @@ import org.hibernate.type.ForeignKeyDirection; + * @author Gail Badner + */ public class StandardAssociationRelationalBindingResolverImpl implements AssociationRelationalBindingResolver { - private final RelationalValueBindingHelper relationalValueBindingHelper; - private final ForeignKeyHelper foreignKeyHelper; - private final TableHelper tableHelper; + private final HelperContext helperContext; - public StandardAssociationRelationalBindingResolverImpl(LocalBindingContext bindingContext) { - this.relationalValueBindingHelper = new RelationalValueBindingHelper( bindingContext ); - this.foreignKeyHelper = new ForeignKeyHelper( bindingContext ); - this.tableHelper = new TableHelper( bindingContext ); + public StandardAssociationRelationalBindingResolverImpl(HelperContext helperContext) { + this.helperContext = helperContext; } @Override @@ -124,15 +120,15 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa throw new AssertionFailure( "Cannot create a foreign key for one-to-one with foreign key direction going to the parent." ); } - final TableSpecification targetTable = foreignKeyHelper.determineForeignKeyTargetTable( + final TableSpecification targetTable = foreignKeyHelper().determineForeignKeyTargetTable( referencedEntityBinding, attributeSource ); - final List targetColumns = foreignKeyHelper.determineForeignKeyTargetColumns( + final List targetColumns = foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, attributeSource ); - return foreignKeyHelper.locateOrCreateForeignKey( + return foreignKeyHelper().locateOrCreateForeignKey( attributeSource.getExplicitForeignKeyName(), sourceTable, sourceColumns, @@ -180,7 +176,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa AttributeBindingContainer attributeBindingContainer, List relationalValueBindings, EntityBinding referencedEntityBinding) { - final List targetColumns = foreignKeyHelper.determineForeignKeyTargetColumns( + final List targetColumns = foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, attributeSource ); @@ -200,7 +196,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa final TableSpecification collectionTable, final EntityBinding referencedEntityBinding) { final List targetColumns = - foreignKeyHelper.determineForeignKeyTargetColumns( + foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, elementSource ); @@ -237,7 +233,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa final List relationalValueBindings, final EntityBinding referencedEntityBinding) { final List targetColumns = - foreignKeyHelper.determineForeignKeyTargetColumns( + foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, elementSource ); @@ -258,7 +254,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa EntityBinding referencedEntityBinding) { final TableSpecificationSource collectionTableSource = pluralAttributeSource.getCollectionTableSpecificationSource(); - return tableHelper.createTable( + return tableHelper().createTable( collectionTableSource, new ManyToManyCollectionTableNamingStrategyHelper( attributePath, @@ -277,7 +273,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa final EntityBinding referencedEntityBinding) { final PluralAttributeKeySource keySource = attributeSource.getKeySource(); - final ListtargetColumns = foreignKeyHelper.determineForeignKeyTargetColumns( + final ListtargetColumns = foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, keySource ); @@ -334,7 +330,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa final EntityBinding referencedEntityBinding) { final PluralAttributeKeySource keySource = attributeSource.getKeySource(); List targetColumns = - foreignKeyHelper.determineForeignKeyTargetColumns( + foreignKeyHelper().determineForeignKeyTargetColumns( referencedEntityBinding, keySource ); @@ -353,7 +349,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa public SingularAttributeBinding resolvePluralAttributeKeyReferencedBinding( AttributeBindingContainer attributeBindingContainer, PluralAttributeSource attributeSource) { - return foreignKeyHelper.determineReferencedAttributeBinding( + return foreignKeyHelper().determineReferencedAttributeBinding( attributeSource.getKeySource(), attributeBindingContainer.seekEntityBinding() ); @@ -363,7 +359,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa private SingularAttributeBinding resolveReferencedAttributeBinding( ToOneAttributeSource attributeSource, EntityBinding referencedEntityBinding) { - return foreignKeyHelper.determineReferencedAttributeBinding( attributeSource, referencedEntityBinding ); + return foreignKeyHelper().determineReferencedAttributeBinding( attributeSource, referencedEntityBinding ); } public List resolveRelationalValueBindings( @@ -372,7 +368,7 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa TableSpecification defaultTable, boolean forceNonNullable, List defaultNamingStrategies) { - return relationalValueBindingHelper.createRelationalValueBindings( + return relationalValueBindingHelper().createRelationalValueBindings( entityBinding, relationalValueSourceContainer, defaultTable, @@ -387,11 +383,11 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa final TableSpecification sourceTable, final List sourceRelationalValueBindings, final List targetColumns) { - final TableSpecification targetTable = foreignKeyHelper.determineForeignKeyTargetTable( + final TableSpecification targetTable = foreignKeyHelper().determineForeignKeyTargetTable( referencedEntityBinding, foreignKeyContributingSource ); - return foreignKeyHelper.locateOrCreateForeignKey( + return foreignKeyHelper().locateOrCreateForeignKey( foreignKeyContributingSource.getExplicitForeignKeyName(), sourceTable, extractColumnsFromRelationalValueBindings( sourceRelationalValueBindings ), @@ -400,6 +396,18 @@ public class StandardAssociationRelationalBindingResolverImpl implements Associa ); } + private TableHelper tableHelper() { + return helperContext.tableHelper(); + } + + private ForeignKeyHelper foreignKeyHelper() { + return helperContext.foreignKeyHelper(); + } + + private RelationalValueBindingHelper relationalValueBindingHelper() { + return helperContext.relationalValueBindingHelper(); + } + // TODO: try to get rid of this... private static List extractColumnsFromRelationalValueBindings( final List valueBindings) {