HHH-6173 the makeXYX methods in EntityBinding now take the actual attribute instead of just the attribute name

This commit is contained in:
Hardy Ferentschik 2011-06-27 15:30:24 +02:00
parent 295e67201c
commit 96b4d410c4
6 changed files with 61 additions and 45 deletions

View File

@ -33,6 +33,7 @@ import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
import org.hibernate.MappingException;
import org.hibernate.metamodel.binding.state.EntityBindingState;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.Entity;
import org.hibernate.metamodel.domain.JavaType;
import org.hibernate.metamodel.relational.Column;
@ -253,34 +254,34 @@ public class EntityBinding {
return entityReferencingAttributeBindings;
}
public SimpleAttributeBinding makeSimpleIdAttributeBinding(String name) {
final SimpleAttributeBinding binding = makeSimpleAttributeBinding( name, true, true );
public SimpleAttributeBinding makeSimpleIdAttributeBinding(Attribute attribute) {
final SimpleAttributeBinding binding = makeSimpleAttributeBinding( attribute, true, true );
getEntityIdentifier().setValueBinding( binding );
return binding;
}
public EntityDiscriminator makeEntityDiscriminator(String attributeName) {
public EntityDiscriminator makeEntityDiscriminator(Attribute attribute) {
if ( entityDiscriminator != null ) {
throw new AssertionFailure( "Creation of entity discriminator was called more than once" );
}
entityDiscriminator = new EntityDiscriminator();
entityDiscriminator.setValueBinding( makeSimpleAttributeBinding( attributeName, true, false ) );
entityDiscriminator.setValueBinding( makeSimpleAttributeBinding( attribute, true, false ) );
return entityDiscriminator;
}
public SimpleAttributeBinding makeVersionBinding(String attributeName) {
versionBinding = makeSimpleAttributeBinding( attributeName, true, false );
public SimpleAttributeBinding makeVersionBinding(Attribute attribute) {
versionBinding = makeSimpleAttributeBinding( attribute, true, false );
return versionBinding;
}
public SimpleAttributeBinding makeSimpleAttributeBinding(String name) {
return makeSimpleAttributeBinding( name, false, false );
public SimpleAttributeBinding makeSimpleAttributeBinding(Attribute attribute) {
return makeSimpleAttributeBinding( attribute, false, false );
}
private SimpleAttributeBinding makeSimpleAttributeBinding(String name, boolean forceNonNullable, boolean forceUnique) {
private SimpleAttributeBinding makeSimpleAttributeBinding(Attribute attribute, boolean forceNonNullable, boolean forceUnique) {
final SimpleAttributeBinding binding = new SimpleAttributeBinding( this, forceNonNullable, forceUnique );
registerAttributeBinding( name, binding );
binding.setAttribute( entity.getAttribute( name ) );
registerAttributeBinding( attribute.getName(), binding );
binding.setAttribute( attribute );
return binding;
}

View File

@ -53,6 +53,7 @@ import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.binding.state.DiscriminatorBindingState;
import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState;
import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.AttributeContainer;
import org.hibernate.metamodel.domain.Hierarchical;
import org.hibernate.metamodel.domain.SingularAttribute;
@ -481,9 +482,9 @@ public class EntityBinder {
throw new AssertionFailure( "Unexpected attribute type for id attribute" );
}
entityBinding.getEntity().getOrCreateSingularAttribute( idName );
Attribute attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idName );
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName );
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( attribute );
attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) );
attributeBinding.initialize( new ColumnRelationalStateImpl( (SimpleAttribute) idAttribute, meta ) );
bindSingleIdGeneratedValue( entityBinding, idName );
@ -624,23 +625,22 @@ public class EntityBinder {
return;
}
String attributeName = simpleAttribute.getName();
container.getOrCreateSingularAttribute( attributeName );
Attribute attribute = container.getOrCreateSingularAttribute( simpleAttribute.getName() );
SimpleAttributeBinding attributeBinding;
if ( simpleAttribute.isDiscriminator() ) {
EntityDiscriminator entityDiscriminator = entityBinding.makeEntityDiscriminator( attributeName );
EntityDiscriminator entityDiscriminator = entityBinding.makeEntityDiscriminator( attribute );
DiscriminatorBindingState bindingState = new DiscriminatorBindingStateImpl( simpleAttribute );
entityDiscriminator.initialize( bindingState );
attributeBinding = entityDiscriminator.getValueBinding();
}
else if ( simpleAttribute.isVersioned() ) {
attributeBinding = entityBinding.makeVersionBinding( attributeName );
attributeBinding = entityBinding.makeVersionBinding( attribute );
SimpleAttributeBindingState bindingState = new AttributeBindingStateImpl( simpleAttribute );
attributeBinding.initialize( bindingState );
}
else {
attributeBinding = entityBinding.makeSimpleAttributeBinding( attributeName );
attributeBinding = entityBinding.makeSimpleAttributeBinding( attribute );
SimpleAttributeBindingState bindingState = new AttributeBindingStateImpl( simpleAttribute );
attributeBinding.initialize( bindingState );
}

View File

@ -37,6 +37,7 @@ import org.hibernate.metamodel.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState;
import org.hibernate.metamodel.binding.state.PluralAttributeBindingState;
import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.Hierarchical;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.relational.Table;
@ -264,7 +265,7 @@ abstract class AbstractEntityBinder {
}
else if ( XMLManyToOneElement.class.isInstance( attribute ) ) {
XMLManyToOneElement manyToOne = XMLManyToOneElement.class.cast( attribute );
attributeBinding = makeManyToOneAttributeBinding( manyToOne, entityBinding );
attributeBinding = makeManyToOneAttributeBinding( manyToOne, entityBinding );
}
else if ( XMLAnyElement.class.isInstance( attribute ) ) {
// todo : implement
@ -397,14 +398,14 @@ PrimitiveArray
)
);
entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
return entityBinding.makeSimpleAttributeBinding( bindingState.getAttributeName() )
Attribute attribute = entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
return entityBinding.makeSimpleAttributeBinding( attribute )
.initialize( bindingState )
.initialize( relationalState );
}
protected static ValueRelationalState convertToSimpleValueRelationalStateIfPossible(ValueRelationalState state) {
// TODO: should a single-valued tuple always be converted???
// TODO: should a single-valued tuple always be converted???
if ( !TupleRelationalState.class.isInstance( state ) ) {
return state;
}
@ -428,10 +429,11 @@ PrimitiveArray
BagBinding collectionBinding = entityBinding.makeBagAttributeBinding(
bindingState.getAttributeName(),
getCollectionElementType( collection ) )
getCollectionElementType( collection )
)
.initialize( bindingState );
// todo : relational model binding
// todo : relational model binding
return collectionBinding;
}
@ -457,7 +459,7 @@ PrimitiveArray
}
private ManyToOneAttributeBinding makeManyToOneAttributeBinding(XMLManyToOneElement manyToOne,
EntityBinding entityBinding) {
EntityBinding entityBinding) {
ManyToOneAttributeBindingState bindingState =
new HbmManyToOneAttributeBindingState(
entityBinding.getEntity().getJavaType().getName(),
@ -468,13 +470,13 @@ PrimitiveArray
// boolean (true here) indicates that by default column names should be guessed
ManyToOneRelationalState relationalState =
new HbmManyToOneRelationalStateContainer(
bindingContext,
true,
manyToOne
);
new HbmManyToOneRelationalStateContainer(
bindingContext,
true,
manyToOne
);
entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
ManyToOneAttributeBinding manyToOneAttributeBinding =
entityBinding.makeManyToOneAttributeBinding( bindingState.getAttributeName() )
.initialize( bindingState )

View File

@ -27,7 +27,9 @@ import org.hibernate.InvalidMappingException;
import org.hibernate.MappingException;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.binding.state.DiscriminatorBindingState;
import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.relational.Identifier;
import org.hibernate.metamodel.relational.InLineView;
import org.hibernate.metamodel.relational.Schema;
@ -39,7 +41,6 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLCompositeId;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId;
import org.hibernate.metamodel.binding.state.DiscriminatorBindingState;
/**
* TODO : javadoc
@ -144,8 +145,8 @@ class RootEntityBinder extends AbstractEntityBinder {
throw new MappingException( "ID is expected to be a single column, but has more than 1 value" );
}
entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeSimpleIdAttributeBinding( bindingState.getAttributeName() )
Attribute attribute = entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeSimpleIdAttributeBinding( attribute )
.initialize( bindingState )
.initialize( relationalStateContainer.getRelationalStates().get( 0 ) );
@ -236,10 +237,10 @@ class RootEntityBinder extends AbstractEntityBinder {
}
DiscriminatorBindingState bindingState = new HbmDiscriminatorBindingState(
entityBinding.getEntity().getJavaType().getName(),
entityBinding.getEntity().getName(),
getBindingContext(),
xmlEntityClazz
entityBinding.getEntity().getJavaType().getName(),
entityBinding.getEntity().getName(),
getBindingContext(),
xmlEntityClazz
);
// boolean (true here) indicates that by default column names should be guessed
@ -252,8 +253,8 @@ class RootEntityBinder extends AbstractEntityBinder {
);
entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeEntityDiscriminator( bindingState.getAttributeName() )
Attribute attribute = entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeEntityDiscriminator( attribute )
.initialize( bindingState )
.initialize( relationalState );
}
@ -294,8 +295,8 @@ class RootEntityBinder extends AbstractEntityBinder {
)
);
entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeVersionBinding( bindingState.getAttributeName() )
Attribute attribute = entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeVersionBinding( attribute )
.initialize( bindingState )
.initialize( relationalState );
}
@ -322,7 +323,8 @@ class RootEntityBinder extends AbstractEntityBinder {
)
);
entityBinding.makeVersionBinding( bindingState.getAttributeName() )
Attribute attribute = entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() );
entityBinding.makeVersionBinding( attribute )
.initialize( bindingState )
.initialize( relationalState );
}

View File

@ -59,7 +59,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
entityBinding.setBaseTable( table );
SingularAttribute idAttribute = entity.getOrCreateSingularAttribute( "id" );
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleAttributeBinding( "id" );
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleAttributeBinding( idAttribute );
attributeBinding.getHibernateTypeDescriptor().setTypeName( "long" );
assertSame( idAttribute, attributeBinding.getAttribute() );

View File

@ -31,8 +31,11 @@ import javax.persistence.Id;
import org.junit.Test;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.Component;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
/**
* Tests for {@code j.p.Embeddable}.
@ -44,7 +47,15 @@ public class EmbeddableBindingTests extends BaseAnnotationBindingTestCase {
public void testEmbeddable() {
buildMetadataSources( User.class, Address.class );
EntityBinding binding = getEntityBinding( User.class );
assertNotNull( binding.getAttributeBinding( "address" ) );
assertNotNull( binding.getAttributeBinding( "street" ) );
assertNotNull( binding.getAttributeBinding( "city" ) );
assertNotNull( binding.getAttributeBinding( "postCode" ) );
Attribute attribute = binding.getEntity().getAttribute( "address" );
assertTrue(
"Wrong container type. Should be a component",
attribute.getAttributeContainer() instanceof Component
);
}
@Entity