HHH-6479 - Split notions of locating and creating an attribute

This commit is contained in:
Steve Ebersole 2011-07-22 14:27:35 -05:00
parent 1de1325e64
commit 7529dd8e81
10 changed files with 138 additions and 182 deletions

View File

@ -675,7 +675,7 @@ public final class SessionFactoryImpl
final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() ); final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
CollectionRegionAccessStrategy accessStrategy = null; CollectionRegionAccessStrategy accessStrategy = null;
if ( accessType != null && settings.isSecondLevelCacheEnabled() ) { if ( accessType != null && settings.isSecondLevelCacheEnabled() ) {
// TODO: is model.getAttribute().getName() the collection's role??? For now, assuming it is // TODO: is model.locateAttribute().getName() the collection's role??? For now, assuming it is
LOG.trace("Building cache for collection data [" + model.getAttribute().getName() + "]"); LOG.trace("Building cache for collection data [" + model.getAttribute().getName() + "]");
CollectionRegion collectionRegion = CollectionRegion collectionRegion =
settings.getRegionFactory() settings.getRegionFactory()
@ -690,7 +690,7 @@ public final class SessionFactoryImpl
serviceRegistry serviceRegistry
.getService( PersisterFactory.class ) .getService( PersisterFactory.class )
.createCollectionPersister( metadata, model, accessStrategy, this ); .createCollectionPersister( metadata, model, accessStrategy, this );
// TODO: is model.getAttribute().getName() the collection's role??? For now, assuming it is // TODO: is model.locateAttribute().getName() the collection's role??? For now, assuming it is
collectionPersisters.put( model.getAttribute().getName(), persister.getCollectionMetadata() ); collectionPersisters.put( model.getAttribute().getName(), persister.getCollectionMetadata() );
Type indexType = persister.getIndexType(); Type indexType = persister.getIndexType();
if ( indexType != null && indexType.isAssociationType() && !indexType.isAnyType() ) { if ( indexType != null && indexType.isAssociationType() && !indexType.isAnyType() ) {

View File

@ -67,21 +67,6 @@ public class EntityBinding {
private Set<FilterDefinition> filterDefinitions = new HashSet<FilterDefinition>(); private Set<FilterDefinition> filterDefinitions = new HashSet<FilterDefinition>();
private Set<SingularAssociationAttributeBinding> entityReferencingAttributeBindings = new HashSet<SingularAssociationAttributeBinding>(); private Set<SingularAssociationAttributeBinding> entityReferencingAttributeBindings = new HashSet<SingularAssociationAttributeBinding>();
/**
* Used to instantiate the EntityBinding for an entity that is the root of an inheritance hierarchy
*
* @param inheritanceType The inheritance type for the hierarchy
* @param entityMode The entity mode used in this hierarchy.
*/
public EntityBinding(InheritanceType inheritanceType, EntityMode entityMode) {
this.superEntityBinding = null;
this.hierarchyDetails = new HierarchyDetails( this, inheritanceType, entityMode );
}
public EntityBinding(EntityBinding superEntityBinding) {
this.superEntityBinding = superEntityBinding;
this.hierarchyDetails = superEntityBinding.getHierarchyDetails();
}
private MetaAttributeContext metaAttributeContext; private MetaAttributeContext metaAttributeContext;
@ -106,6 +91,27 @@ public class EntityBinding {
private Set<String> synchronizedTableNames = new HashSet<String>(); private Set<String> synchronizedTableNames = new HashSet<String>();
/**
* Used to instantiate the EntityBinding for an entity that is the root of an inheritance hierarchy
*
* @param inheritanceType The inheritance type for the hierarchy
* @param entityMode The entity mode used in this hierarchy.
*/
public EntityBinding(InheritanceType inheritanceType, EntityMode entityMode) {
this.superEntityBinding = null;
this.hierarchyDetails = new HierarchyDetails( this, inheritanceType, entityMode );
}
/**
* Used to instantiate the EntityBinding for an entity that is a subclass (sub-entity) in an inheritance hierarchy
*
* @param superEntityBinding The entity binding of this binding's super
*/
public EntityBinding(EntityBinding superEntityBinding) {
this.superEntityBinding = superEntityBinding;
this.hierarchyDetails = superEntityBinding.getHierarchyDetails();
}
public HierarchyDetails getHierarchyDetails() { public HierarchyDetails getHierarchyDetails() {
return hierarchyDetails; return hierarchyDetails;

View File

@ -194,7 +194,7 @@ public class ManyToOneAttributeBinding extends SimpleSingularAttributeBinding im
// if ( getCascadeTypes().contains( CascadeType.DELETE_ORPHAN ) ) { // if ( getCascadeTypes().contains( CascadeType.DELETE_ORPHAN ) ) {
// if ( !isLogicalOneToOne ) { // if ( !isLogicalOneToOne ) {
// throw new MappingException( // throw new MappingException(
// "many-to-one attribute [" + getAttribute().getName() + "] does not support orphan delete as it is not unique" // "many-to-one attribute [" + locateAttribute().getName() + "] does not support orphan delete as it is not unique"
// ); // );
// } // }
// } // }

View File

@ -28,7 +28,6 @@ import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.internal.util.Value; import org.hibernate.internal.util.Value;
@ -80,172 +79,97 @@ public abstract class AbstractAttributeContainer implements AttributeContainer,
} }
@Override @Override
public Set<Attribute> getAttributes() { public Set<Attribute> attributes() {
return Collections.unmodifiableSet( attributeSet ); return Collections.unmodifiableSet( attributeSet );
} }
@Override @Override
public Attribute getAttribute(String name) { public Attribute locateAttribute(String name) {
return attributeMap.get( name ); return attributeMap.get( name );
} }
@Override @Override
public SingularAttribute locateOrCreateSingularAttribute(String name) { public SingularAttribute locateSingularAttribute(String name) {
SingularAttribute attribute = (SingularAttribute) getAttribute( name ); return (SingularAttribute) locateAttribute( name );
if ( attribute == null ) {
attribute = new SingularAttributeImpl( name, this );
addAttribute( attribute );
} }
@Override
public SingularAttribute createSingularAttribute(String name) {
SingularAttribute attribute = new SingularAttributeImpl( name, this );
addAttribute( attribute );
return attribute; return attribute;
} }
@Override @Override
public SingularAttribute locateOrCreateVirtualAttribute(String name) { public SingularAttribute createVirtualSingularAttribute(String name) {
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
@Override @Override
public SingularAttribute locateOrCreateComponentAttribute(String name) { public SingularAttribute locateComponentAttribute(String name) {
SingularAttributeImpl attribute = (SingularAttributeImpl) getAttribute( name ); return (SingularAttributeImpl) locateAttribute( name );
if ( attribute == null ) {
ComponentAttributeContainerDelegate component = new ComponentAttributeContainerDelegate();
attribute = new SingularAttributeImpl( name, component );
component.singularAttribute = attribute;
addAttribute( attribute );
} }
@Override
public SingularAttribute createComponentAttribute(String name, Component component) {
SingularAttributeImpl attribute = new SingularAttributeImpl( name, component );
addAttribute( attribute );
return attribute; return attribute;
} }
private static class ComponentAttributeContainerDelegate implements AttributeContainer {
private SingularAttributeImpl singularAttribute;
private ComponentAttributeContainerDelegate() {
}
private Component realComponent() {
if ( singularAttribute.getSingularAttributeType() == null ) {
throw new HibernateException( "Component type was not yet bound" );
}
if ( ! Component.class.isInstance( singularAttribute.getSingularAttributeType() ) ) {
throw new HibernateException( "Unexpected bound type for component attribute" );
}
return (Component) singularAttribute.getSingularAttributeType();
}
@Override @Override
public Attribute getAttribute(String name) { public PluralAttribute locatePluralAttribute(String name) {
return realComponent().getAttribute( name ); return (PluralAttribute) locateAttribute( name );
} }
@Override protected PluralAttribute createPluralAttribute(String name, PluralAttributeNature nature) {
public Set<Attribute> getAttributes() { PluralAttribute attribute = nature.isIndexed()
return realComponent().getAttributes();
}
@Override
public SingularAttribute locateOrCreateSingularAttribute(String name) {
return realComponent().locateOrCreateSingularAttribute( name );
}
@Override
public SingularAttribute locateOrCreateVirtualAttribute(String name) {
return realComponent().locateOrCreateVirtualAttribute( name );
}
@Override
public PluralAttribute locateOrCreatePluralAttribute(String name, PluralAttributeNature nature) {
return realComponent().locateOrCreatePluralAttribute( name, nature );
}
@Override
public PluralAttribute locateOrCreateBag(String name) {
return realComponent().locateOrCreateBag( name );
}
@Override
public PluralAttribute locateOrCreateSet(String name) {
return realComponent().locateOrCreateBag( name );
}
@Override
public IndexedPluralAttribute locateOrCreateList(String name) {
return realComponent().locateOrCreateList( name );
}
@Override
public IndexedPluralAttribute locateOrCreateMap(String name) {
return realComponent().locateOrCreateMap( name );
}
@Override
public SingularAttribute locateOrCreateComponentAttribute(String name) {
return realComponent().locateOrCreateComponentAttribute( name );
}
@Override
public String getName() {
return realComponent().getName();
}
@Override
public String getClassName() {
return realComponent().getClassName();
}
@Override
public Class<?> getClassReference() {
return realComponent().getClassReference();
}
@Override
public Value<Class<?>> getClassReferenceUnresolved() {
return realComponent().getClassReferenceUnresolved();
}
@Override
public boolean isAssociation() {
return realComponent().isAssociation();
}
@Override
public boolean isComponent() {
return realComponent().isComponent();
}
}
@Override
public PluralAttribute locateOrCreateBag(String name) {
return locateOrCreatePluralAttribute( name, PluralAttributeNature.BAG );
}
@Override
public PluralAttribute locateOrCreateSet(String name) {
return locateOrCreatePluralAttribute( name, PluralAttributeNature.SET );
}
@Override
public IndexedPluralAttribute locateOrCreateList(String name) {
return (IndexedPluralAttribute) locateOrCreatePluralAttribute( name, PluralAttributeNature.LIST );
}
@Override
public IndexedPluralAttribute locateOrCreateMap(String name) {
return (IndexedPluralAttribute) locateOrCreatePluralAttribute( name, PluralAttributeNature.MAP );
}
@Override
public PluralAttribute locateOrCreatePluralAttribute(String name, PluralAttributeNature nature) {
PluralAttribute attribute = (PluralAttribute) getAttribute( name );
if ( attribute == null ) {
attribute = nature.isIndexed()
? new IndexedPluralAttributeImpl( name, nature, this ) ? new IndexedPluralAttributeImpl( name, nature, this )
: new PluralAttributeImpl( name, nature, this ); : new PluralAttributeImpl( name, nature, this );
addAttribute( attribute ); addAttribute( attribute );
}
return attribute; return attribute;
} }
@Override
public PluralAttribute locateBag(String name) {
return locatePluralAttribute( name );
}
@Override
public PluralAttribute createBag(String name) {
return createPluralAttribute( name, PluralAttributeNature.BAG );
}
@Override
public PluralAttribute locateSet(String name) {
return locatePluralAttribute( name );
}
@Override
public PluralAttribute createSet(String name) {
return createPluralAttribute( name, PluralAttributeNature.SET );
}
@Override
public IndexedPluralAttribute locateList(String name) {
return (IndexedPluralAttribute) locatePluralAttribute( name );
}
@Override
public IndexedPluralAttribute createList(String name) {
return (IndexedPluralAttribute) createPluralAttribute( name, PluralAttributeNature.LIST );
}
@Override
public IndexedPluralAttribute locateMap(String name) {
return (IndexedPluralAttribute) locatePluralAttribute( name );
}
@Override
public IndexedPluralAttribute createMap(String name) {
return (IndexedPluralAttribute) createPluralAttribute( name, PluralAttributeNature.MAP );
}
@Override @Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();

View File

@ -39,23 +39,34 @@ public interface AttributeContainer extends Type {
* *
* @return The attribute matching the given name, or null. * @return The attribute matching the given name, or null.
*/ */
public Attribute getAttribute(String name); public Attribute locateAttribute(String name);
/** /**
* Retrieve the attributes contained in this container. * Retrieve the attributes contained in this container.
* *
* @return The contained attributes * @return The contained attributes
*/ */
public Set<Attribute> getAttributes(); public Set<Attribute> attributes();
public SingularAttribute locateOrCreateSingularAttribute(String name); public SingularAttribute locateSingularAttribute(String name);
public SingularAttribute locateOrCreateVirtualAttribute(String name); public SingularAttribute createSingularAttribute(String name);
public SingularAttribute createVirtualSingularAttribute(String name);
public PluralAttribute locateOrCreatePluralAttribute(String name, PluralAttributeNature nature); public SingularAttribute locateComponentAttribute(String name);
public PluralAttribute locateOrCreateBag(String name); public SingularAttribute createComponentAttribute(String name, Component component);
public PluralAttribute locateOrCreateSet(String name);
public IndexedPluralAttribute locateOrCreateList(String name); public PluralAttribute locatePluralAttribute(String name);
public IndexedPluralAttribute locateOrCreateMap(String name);
public PluralAttribute locateBag(String name);
public PluralAttribute createBag(String name);
public PluralAttribute locateSet(String name);
public PluralAttribute createSet(String name);
public IndexedPluralAttribute locateList(String name);
public IndexedPluralAttribute createList(String name);
public IndexedPluralAttribute locateMap(String name);
public IndexedPluralAttribute createMap(String name);
public SingularAttribute locateOrCreateComponentAttribute(String name);
} }

View File

@ -392,7 +392,7 @@ public class Binder {
if ( attributeSource.isSingular() ) { if ( attributeSource.isSingular() ) {
final SingularAttributeSource singularAttributeSource = (SingularAttributeSource) attributeSource; final SingularAttributeSource singularAttributeSource = (SingularAttributeSource) attributeSource;
if ( singularAttributeSource.getNature() == SingularAttributeNature.COMPONENT ) { if ( singularAttributeSource.getNature() == SingularAttributeNature.COMPONENT ) {
throw new NotYetImplementedException( "Component binding not yet implemented :(" ); bindComponent( singularAttributeSource, entityBinding );
} }
else { else {
doBasicSingularAttributeBindingCreation( singularAttributeSource, entityBinding ); doBasicSingularAttributeBindingCreation( singularAttributeSource, entityBinding );
@ -404,13 +404,20 @@ public class Binder {
} }
} }
private void bindComponent(SingularAttributeSource singularAttributeSource, EntityBinding entityBinding) {
throw new NotYetImplementedException( "Component binding not yet implemented :(" );
}
private void bindPersistentCollection(PluralAttributeSource attributeSource, EntityBinding entityBinding) { private void bindPersistentCollection(PluralAttributeSource attributeSource, EntityBinding entityBinding) {
final PluralAttribute existingAttribute = entityBinding.getEntity().locatePluralAttribute( attributeSource.getName() );
final AbstractPluralAttributeBinding pluralAttributeBinding; final AbstractPluralAttributeBinding pluralAttributeBinding;
if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.BAG ) { if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.BAG ) {
final PluralAttribute pluralAttribute = entityBinding.getEntity() final PluralAttribute attribute = existingAttribute != null
.locateOrCreateBag( attributeSource.getName() ); ? existingAttribute
: entityBinding.getEntity().createBag( attributeSource.getName() );
pluralAttributeBinding = entityBinding.makeBagAttributeBinding( pluralAttributeBinding = entityBinding.makeBagAttributeBinding(
pluralAttribute, attribute,
convert( attributeSource.getPluralAttributeElementNature() ) convert( attributeSource.getPluralAttributeElementNature() )
); );
} }
@ -434,9 +441,17 @@ public class Binder {
private SimpleSingularAttributeBinding doBasicSingularAttributeBindingCreation( private SimpleSingularAttributeBinding doBasicSingularAttributeBindingCreation(
SingularAttributeSource attributeSource, SingularAttributeSource attributeSource,
EntityBinding entityBinding) { EntityBinding entityBinding) {
final SingularAttribute attribute = attributeSource.isVirtualAttribute() final SingularAttribute existingAttribute = entityBinding.getEntity().locateSingularAttribute( attributeSource.getName() );
? entityBinding.getEntity().locateOrCreateVirtualAttribute( attributeSource.getName() ) final SingularAttribute attribute;
: entityBinding.getEntity().locateOrCreateSingularAttribute( attributeSource.getName() ); if ( existingAttribute != null ) {
attribute = existingAttribute;
}
else if ( attributeSource.isVirtualAttribute() ) {
attribute = entityBinding.getEntity().createVirtualSingularAttribute( attributeSource.getName() );
}
else {
attribute = entityBinding.getEntity().createSingularAttribute( attributeSource.getName() );
}
final SimpleSingularAttributeBinding attributeBinding; final SimpleSingularAttributeBinding attributeBinding;
if ( attributeSource.getNature() == SingularAttributeNature.BASIC ) { if ( attributeSource.getNature() == SingularAttributeNature.BASIC ) {

View File

@ -702,7 +702,7 @@ public class EntityMetamodel implements Serializable {
( ( SingularAttribute ) attribute ).getSingularAttributeType().isComponent() ) { ( ( SingularAttribute ) attribute ).getSingularAttributeType().isComponent() ) {
org.hibernate.metamodel.domain.Component component = org.hibernate.metamodel.domain.Component component =
( org.hibernate.metamodel.domain.Component ) ( ( SingularAttribute ) attribute ).getSingularAttributeType(); ( org.hibernate.metamodel.domain.Component ) ( ( SingularAttribute ) attribute ).getSingularAttributeType();
for ( Attribute subAttribute : component.getAttributes() ) { for ( Attribute subAttribute : component.attributes() ) {
propertyIndexes.put( propertyIndexes.put(
attribute.getName() + '.' + subAttribute.getName(), attribute.getName() + '.' + subAttribute.getName(),
i i

View File

@ -60,7 +60,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
entityBinding.setEntity( entity ); entityBinding.setEntity( entity );
entityBinding.setBaseTable( table ); entityBinding.setBaseTable( table );
SingularAttribute idAttribute = entity.locateOrCreateSingularAttribute( "id" ); SingularAttribute idAttribute = entity.createSingularAttribute( "id" );
SimpleSingularAttributeBinding attributeBinding = entityBinding.makeSimpleAttributeBinding( idAttribute ); SimpleSingularAttributeBinding attributeBinding = entityBinding.makeSimpleAttributeBinding( idAttribute );
attributeBinding.getHibernateTypeDescriptor().setExplicitTypeName( "long" ); attributeBinding.getHibernateTypeDescriptor().setExplicitTypeName( "long" );
assertSame( idAttribute, attributeBinding.getAttribute() ); assertSame( idAttribute, attributeBinding.getAttribute() );

View File

@ -53,7 +53,7 @@ public class EmbeddableBindingTests extends BaseAnnotationBindingTestCase {
assertNotNull( binding.getAttributeBinding( "city" ) ); assertNotNull( binding.getAttributeBinding( "city" ) );
assertNotNull( binding.getAttributeBinding( "postCode" ) ); assertNotNull( binding.getAttributeBinding( "postCode" ) );
SingularAttribute attribute = (SingularAttribute) binding.getEntity().getAttribute( "address" ); SingularAttribute attribute = (SingularAttribute) binding.getEntity().locateAttribute( "address" );
assertTrue( assertTrue(
"Wrong container type. Should be a component", "Wrong container type. Should be a component",
attribute.getSingularAttributeType() instanceof Component attribute.getSingularAttributeType() instanceof Component

View File

@ -93,7 +93,7 @@ public class SingularAttributePath<X> extends AbstractPathImpl<X> implements Ser
@Override @Override
protected Attribute locateAttributeInternal(String attributeName) { protected Attribute locateAttributeInternal(String attributeName) {
final Attribute attribute = managedType.getAttribute( attributeName ); final Attribute attribute = managedType.getAttribute( attributeName );
// ManagedType.getAttribute should throw exception rather than return // ManagedType.locateAttribute should throw exception rather than return
// null, but just to be safe... // null, but just to be safe...
if ( attribute == null ) { if ( attribute == null ) {
throw new IllegalArgumentException( "Could not resolve attribute named " + attributeName ); throw new IllegalArgumentException( "Could not resolve attribute named " + attributeName );