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() );
CollectionRegionAccessStrategy accessStrategy = null;
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() + "]");
CollectionRegion collectionRegion =
settings.getRegionFactory()
@ -690,7 +690,7 @@ public final class SessionFactoryImpl
serviceRegistry
.getService( PersisterFactory.class )
.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() );
Type indexType = persister.getIndexType();
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<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;
@ -106,6 +91,27 @@ public class EntityBinding {
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() {
return hierarchyDetails;

View File

@ -194,7 +194,7 @@ public class ManyToOneAttributeBinding extends SimpleSingularAttributeBinding im
// if ( getCascadeTypes().contains( CascadeType.DELETE_ORPHAN ) ) {
// if ( !isLogicalOneToOne ) {
// 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.Set;
import org.hibernate.HibernateException;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.internal.util.Value;
@ -80,172 +79,97 @@ public abstract class AbstractAttributeContainer implements AttributeContainer,
}
@Override
public Set<Attribute> getAttributes() {
public Set<Attribute> attributes() {
return Collections.unmodifiableSet( attributeSet );
}
@Override
public Attribute getAttribute(String name) {
public Attribute locateAttribute(String name) {
return attributeMap.get( name );
}
@Override
public SingularAttribute locateOrCreateSingularAttribute(String name) {
SingularAttribute attribute = (SingularAttribute) getAttribute( name );
if ( attribute == null ) {
public SingularAttribute locateSingularAttribute(String name) {
return (SingularAttribute) locateAttribute( name );
}
attribute = new SingularAttributeImpl( name, this );
addAttribute( attribute );
}
@Override
public SingularAttribute createSingularAttribute(String name) {
SingularAttribute attribute = new SingularAttributeImpl( name, this );
addAttribute( attribute );
return attribute;
}
@Override
public SingularAttribute locateOrCreateVirtualAttribute(String name) {
public SingularAttribute createVirtualSingularAttribute(String name) {
throw new NotYetImplementedException();
}
@Override
public SingularAttribute locateOrCreateComponentAttribute(String name) {
SingularAttributeImpl attribute = (SingularAttributeImpl) getAttribute( name );
if ( attribute == null ) {
ComponentAttributeContainerDelegate component = new ComponentAttributeContainerDelegate();
attribute = new SingularAttributeImpl( name, component );
component.singularAttribute = attribute;
addAttribute( attribute );
}
public SingularAttribute locateComponentAttribute(String name) {
return (SingularAttributeImpl) locateAttribute( name );
}
@Override
public SingularAttribute createComponentAttribute(String name, Component component) {
SingularAttributeImpl attribute = new SingularAttributeImpl( name, component );
addAttribute( 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
public Attribute getAttribute(String name) {
return realComponent().getAttribute( name );
}
@Override
public Set<Attribute> getAttributes() {
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 locatePluralAttribute(String name) {
return (PluralAttribute) locateAttribute( name );
}
@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 PluralAttributeImpl( name, nature, this );
addAttribute( attribute );
}
protected PluralAttribute createPluralAttribute(String name, PluralAttributeNature nature) {
PluralAttribute attribute = nature.isIndexed()
? new IndexedPluralAttributeImpl( name, nature, this )
: new PluralAttributeImpl( name, nature, this );
addAttribute( 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
public String toString() {
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.
*/
public Attribute getAttribute(String name);
public Attribute locateAttribute(String name);
/**
* Retrieve the attributes contained in this container.
*
* @return The contained attributes
*/
public Set<Attribute> getAttributes();
public Set<Attribute> attributes();
public SingularAttribute locateOrCreateSingularAttribute(String name);
public SingularAttribute locateOrCreateVirtualAttribute(String name);
public SingularAttribute locateSingularAttribute(String name);
public SingularAttribute createSingularAttribute(String name);
public SingularAttribute createVirtualSingularAttribute(String name);
public PluralAttribute locateOrCreatePluralAttribute(String name, PluralAttributeNature nature);
public PluralAttribute locateOrCreateBag(String name);
public PluralAttribute locateOrCreateSet(String name);
public IndexedPluralAttribute locateOrCreateList(String name);
public IndexedPluralAttribute locateOrCreateMap(String name);
public SingularAttribute locateComponentAttribute(String name);
public SingularAttribute createComponentAttribute(String name, Component component);
public PluralAttribute locatePluralAttribute(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() ) {
final SingularAttributeSource singularAttributeSource = (SingularAttributeSource) attributeSource;
if ( singularAttributeSource.getNature() == SingularAttributeNature.COMPONENT ) {
throw new NotYetImplementedException( "Component binding not yet implemented :(" );
bindComponent( singularAttributeSource, entityBinding );
}
else {
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) {
final PluralAttribute existingAttribute = entityBinding.getEntity().locatePluralAttribute( attributeSource.getName() );
final AbstractPluralAttributeBinding pluralAttributeBinding;
if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.BAG ) {
final PluralAttribute pluralAttribute = entityBinding.getEntity()
.locateOrCreateBag( attributeSource.getName() );
if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.BAG ) {
final PluralAttribute attribute = existingAttribute != null
? existingAttribute
: entityBinding.getEntity().createBag( attributeSource.getName() );
pluralAttributeBinding = entityBinding.makeBagAttributeBinding(
pluralAttribute,
attribute,
convert( attributeSource.getPluralAttributeElementNature() )
);
}
@ -434,9 +441,17 @@ public class Binder {
private SimpleSingularAttributeBinding doBasicSingularAttributeBindingCreation(
SingularAttributeSource attributeSource,
EntityBinding entityBinding) {
final SingularAttribute attribute = attributeSource.isVirtualAttribute()
? entityBinding.getEntity().locateOrCreateVirtualAttribute( attributeSource.getName() )
: entityBinding.getEntity().locateOrCreateSingularAttribute( attributeSource.getName() );
final SingularAttribute existingAttribute = entityBinding.getEntity().locateSingularAttribute( attributeSource.getName() );
final SingularAttribute attribute;
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;
if ( attributeSource.getNature() == SingularAttributeNature.BASIC ) {

View File

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

View File

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

View File

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

View File

@ -93,7 +93,7 @@ public class SingularAttributePath<X> extends AbstractPathImpl<X> implements Ser
@Override
protected Attribute locateAttributeInternal(String 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...
if ( attribute == null ) {
throw new IllegalArgumentException( "Could not resolve attribute named " + attributeName );