HHH-6173 Creating domain component for embeddables

This commit is contained in:
Hardy Ferentschik 2011-06-27 12:45:43 +02:00
parent d86ce7e68a
commit 295e67201c
7 changed files with 72 additions and 32 deletions

View File

@ -70,12 +70,24 @@ public abstract class AbstractAttributeContainer implements AttributeContainer,
public SingularAttribute getOrCreateSingularAttribute(String name) {
SingularAttribute attribute = (SingularAttribute) getAttribute( name );
if ( attribute == null ) {
attribute = new SingularAttributeImpl( name, this );
addAttribute( attribute );
}
return attribute;
}
@Override
public SingularAttribute getOrCreateComponentAttribute(String name) {
SingularAttribute attribute = (SingularAttribute) getAttribute( name );
if ( attribute == null ) {
Component component = new Component( name, null );
attribute = new SingularAttributeImpl( name, component );
addAttribute( attribute );
}
return attribute;
}
@Override
public PluralAttribute getOrCreateBag(String name) {
return getOrCreatePluralAttribute( name, PluralAttributeNature.BAG );

View File

@ -39,8 +39,19 @@ public interface AttributeContainer extends Type {
*/
public Set<Attribute> getAttributes();
/**
* Retrieve an attribute by name.
*
* @param name The name of the attribute to retrieve.
*
* @return The attribute matching the given name, or null.
*/
public Attribute getAttribute(String name);
public SingularAttribute getOrCreateSingularAttribute(String name);
public SingularAttribute getOrCreateComponentAttribute(String name);
public PluralAttribute getOrCreatePluralAttribute(String name, PluralAttributeNature nature);
public PluralAttribute getOrCreateBag(String name);
@ -50,13 +61,4 @@ public interface AttributeContainer extends Type {
public IndexedPluralAttribute getOrCreateList(String name);
public IndexedPluralAttribute getOrCreateMap(String name);
/**
* Retrieve an attribute by name.
*
* @param name The name of the attribute to retrieve.
*
* @return The attribute matching the given name, or null.
*/
public Attribute getAttribute(String name);
}

View File

@ -152,11 +152,11 @@ public class ConfiguredClass {
return mappedAttributes.values();
}
public Iterable<EmbeddedClass> getEmbeddedClasses() {
return embeddedClasses.values();
public Map<String, EmbeddedClass> getEmbeddedClasses() {
return embeddedClasses;
}
public MappedAttribute getMappedProperty(String propertyName) {
public MappedAttribute getMappedAttribute(String propertyName) {
return mappedAttributes.get( propertyName );
}

View File

@ -23,10 +23,10 @@
*/
package org.hibernate.metamodel.source.annotations.entity;
import javax.persistence.GenerationType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.GenerationType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
@ -53,7 +53,9 @@ 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.AttributeContainer;
import org.hibernate.metamodel.domain.Hierarchical;
import org.hibernate.metamodel.domain.SingularAttribute;
import org.hibernate.metamodel.relational.Identifier;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.relational.TableSpecification;
@ -119,6 +121,9 @@ public class EntityBinder {
// bind all attributes - simple as well as associations
bindAttributes( entityBinding );
bindEmbeddedAttributes( entityBinding );
bindTableUniqueConstraints( entityBinding );
// last, but not least we initialize and register the new EntityBinding
@ -189,7 +194,7 @@ public class EntityBinder {
);
SimpleAttribute discriminatorAttribute = SimpleAttribute.createDiscriminatorAttribute( typeAnnotations );
bindSingleMappedAttribute( entityBinding, discriminatorAttribute );
bindSingleMappedAttribute( entityBinding, entityBinding.getEntity(), discriminatorAttribute );
if ( !( discriminatorAttribute.getColumnValues() instanceof DiscriminatorColumnValues ) ) {
throw new AssertionFailure( "Expected discriminator column values" );
@ -471,7 +476,7 @@ public class EntityBinder {
);
String idName = JandexHelper.getPropertyName( idAnnotation.target() );
MappedAttribute idAttribute = entityClass.getMappedProperty( idName );
MappedAttribute idAttribute = entityClass.getMappedAttribute( idName );
if ( !( idAttribute instanceof SimpleAttribute ) ) {
throw new AssertionFailure( "Unexpected attribute type for id attribute" );
}
@ -547,29 +552,50 @@ public class EntityBinder {
private void bindAttributes(EntityBinding entityBinding) {
for ( MappedAttribute mappedAttribute : entityClass.getMappedAttributes() ) {
if ( mappedAttribute instanceof AssociationAttribute ) {
bindAssociationAttribute( entityBinding, (AssociationAttribute) mappedAttribute );
bindAssociationAttribute(
entityBinding,
entityBinding.getEntity(),
(AssociationAttribute) mappedAttribute
);
}
else {
bindSingleMappedAttribute( entityBinding, (SimpleAttribute) mappedAttribute );
bindSingleMappedAttribute(
entityBinding,
entityBinding.getEntity(),
(SimpleAttribute) mappedAttribute
);
}
}
}
for ( EmbeddedClass embeddedClass : entityClass.getEmbeddedClasses() ) {
private void bindEmbeddedAttributes(EntityBinding entityBinding) {
for ( Map.Entry<String, EmbeddedClass> entry : entityClass.getEmbeddedClasses().entrySet() ) {
String attributeName = entry.getKey();
EmbeddedClass embeddedClass = entry.getValue();
SingularAttribute component = entityBinding.getEntity().getOrCreateComponentAttribute( attributeName );
for ( MappedAttribute mappedAttribute : embeddedClass.getMappedAttributes() ) {
if ( mappedAttribute instanceof AssociationAttribute ) {
bindAssociationAttribute( entityBinding, (AssociationAttribute) mappedAttribute );
bindAssociationAttribute(
entityBinding,
component.getAttributeContainer(),
(AssociationAttribute) mappedAttribute
);
}
else {
bindSingleMappedAttribute( entityBinding, (SimpleAttribute) mappedAttribute );
bindSingleMappedAttribute(
entityBinding,
component.getAttributeContainer(),
(SimpleAttribute) mappedAttribute
);
}
}
}
}
private void bindAssociationAttribute(EntityBinding entityBinding, AssociationAttribute associationAttribute) {
private void bindAssociationAttribute(EntityBinding entityBinding, AttributeContainer container, AssociationAttribute associationAttribute) {
switch ( associationAttribute.getAssociationType() ) {
case MANY_TO_ONE: {
entityBinding.getEntity().getOrCreateSingularAttribute( associationAttribute.getName() );
container.getOrCreateSingularAttribute( associationAttribute.getName() );
ManyToOneAttributeBinding manyToOneAttributeBinding = entityBinding.makeManyToOneAttributeBinding(
associationAttribute.getName()
);
@ -593,13 +619,13 @@ public class EntityBinder {
}
}
private void bindSingleMappedAttribute(EntityBinding entityBinding, SimpleAttribute simpleAttribute) {
private void bindSingleMappedAttribute(EntityBinding entityBinding, AttributeContainer container, SimpleAttribute simpleAttribute) {
if ( simpleAttribute.isId() ) {
return;
}
String attributeName = simpleAttribute.getName();
entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
container.getOrCreateSingularAttribute( attributeName );
SimpleAttributeBinding attributeBinding;
if ( simpleAttribute.isDiscriminator() ) {

View File

@ -44,7 +44,7 @@ public class EmbeddableBindingTests extends BaseAnnotationBindingTestCase {
public void testEmbeddable() {
buildMetadataSources( User.class, Address.class );
EntityBinding binding = getEntityBinding( User.class );
assertNotNull( binding.getAttributeBinding( "city" ) );
assertNotNull( binding.getAttributeBinding( "address" ) );
}
@Entity

View File

@ -81,7 +81,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase {
ConfiguredClass configuredClass = iter.next();
ClassInfo info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() );
MappedAttribute property = configuredClass.getMappedProperty( "value" );
MappedAttribute property = configuredClass.getMappedAttribute( "value" );
assertEquals( Price.class.getName(), property.getType() );
assertTrue( iter.hasNext() );
@ -97,9 +97,9 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase {
info = configuredClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( Item.class.getName() ), info.name() );
// properties are alphabetically ordered!
property = configuredClass.getMappedProperty( "owner" );
property = configuredClass.getMappedAttribute( "owner" );
assertEquals( SomeGuy.class.getName(), property.getType() );
property = configuredClass.getMappedProperty( "type" );
property = configuredClass.getMappedAttribute( "type" );
assertEquals( PaperType.class.getName(), property.getType() );
assertTrue( iter.hasNext() );

View File

@ -75,13 +75,13 @@ public class TypeDiscoveryTest extends BaseUnitTestCase {
Iterator<EntityClass> iter = hierarchies.iterator().next().iterator();
ConfiguredClass configuredClass = iter.next();
MappedAttribute property = configuredClass.getMappedProperty( "id" );
MappedAttribute property = configuredClass.getMappedAttribute( "id" );
assertEquals( "Unexpected property type", "int", property.getType() );
property = configuredClass.getMappedProperty( "string" );
property = configuredClass.getMappedAttribute( "string" );
assertEquals( "Unexpected property type", String.class.getName(), property.getType() );
property = configuredClass.getMappedProperty( "customString" );
property = configuredClass.getMappedAttribute( "customString" );
assertEquals( "Unexpected property type", "my.custom.Type", property.getType() );
Map<String, String> typeParameters = property.getTypeParameters();