From 425f41f5d67b0785244f50688c5f1f18112ad37c Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Fri, 29 Jul 2011 12:43:37 +0200 Subject: [PATCH] HHH-6495 Implementing ComponentAttributeSource#getPath and re-enabling EmbeddableBinding test. Still not sure about all the different implementation in ComponentAttributeSourceImpl. Most of them just don't seem to be relevant. --- .../entity/ComponentAttributeSourceImpl.java | 75 ++++++++++--------- .../annotations/entity/ConfiguredClass.java | 4 + .../entity/EmbeddableBindingTest.java | 33 ++++---- 3 files changed, 63 insertions(+), 49 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ComponentAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ComponentAttributeSourceImpl.java index ef63111716..699804367c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ComponentAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ComponentAttributeSourceImpl.java @@ -42,15 +42,25 @@ import org.hibernate.metamodel.source.binder.RelationalValueSource; import org.hibernate.metamodel.source.binder.SingularAttributeNature; /** + * Annotation backed implementation of {@code ComponentAttributeSource}. + * * @author Steve Ebersole + * @author Hardy Ferentschik */ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { private final EmbeddableClass embeddableClass; private final Value> classReference; + private final String path; public ComponentAttributeSourceImpl(EmbeddableClass embeddableClass) { this.embeddableClass = embeddableClass; this.classReference = new Value>( embeddableClass.getClass() ); + String tmpPath = embeddableClass.getEmbeddedAttributeName(); + ConfiguredClass parent = embeddableClass.getParent(); + while ( parent != null && parent instanceof EmbeddableClass ) { + tmpPath = ( (EmbeddableClass) parent ).getEmbeddedAttributeName() + "." + tmpPath; + } + path = tmpPath; } @Override @@ -79,16 +89,18 @@ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { } @Override - public String getParentReferenceAttributeName() { - // see HHH-6501 - return null; + public String getName() { + return embeddableClass.getEmbeddedAttributeName(); } @Override - public String getPath() { - // todo : implement - // do not see how this is possible currently given how annotations currently handle components - return null; + public String getPropertyAccessorName() { + return embeddableClass.getClassAccessType().toString().toLowerCase(); + } + + @Override + public LocalBindingContext getLocalBindingContext() { + return embeddableClass.getLocalBindingContext(); } @Override @@ -107,8 +119,26 @@ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { } @Override - public LocalBindingContext getLocalBindingContext() { - return embeddableClass.getLocalBindingContext(); + public String getPath() { + return path; + } + + @Override + public Iterable metaAttributes() { + // not relevant for annotations + return Collections.emptySet(); + } + + @Override + public List relationalValueSources() { + // none, they are defined on the simple sub-attributes + return null; + } + + @Override + public String getParentReferenceAttributeName() { + // see HHH-6501 + return null; } @Override @@ -117,32 +147,18 @@ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { return null; } - @Override - public String getName() { - return embeddableClass.getEmbeddedAttributeName(); - } - - @Override - public String getPropertyAccessorName() { - // todo : implement - return null; - } - @Override public boolean isInsertable() { - // todo : implement return true; } @Override public boolean isUpdatable() { - // todo : implement return true; } @Override public PropertyGeneration getGeneration() { - // todo : implement return null; } @@ -158,31 +174,18 @@ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { return true; } - @Override - public Iterable metaAttributes() { - return Collections.emptySet(); - } - @Override public boolean areValuesIncludedInInsertByDefault() { - // todo : implement return true; } @Override public boolean areValuesIncludedInUpdateByDefault() { - // todo : implement return true; } @Override public boolean areValuesNullableByDefault() { - // todo : implement return true; } - - @Override - public List relationalValueSources() { - return null; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java index 234b1df09b..bb707ec377 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java @@ -200,6 +200,10 @@ public class ConfiguredClass { return attributeOverrideMap; } + public AccessType getClassAccessType() { + return classAccessType; + } + @Override public String toString() { final StringBuilder sb = new StringBuilder(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableBindingTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableBindingTest.java index c90fff3293..d4668242b1 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableBindingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableBindingTest.java @@ -30,34 +30,41 @@ import javax.persistence.Id; import org.junit.Test; +import org.hibernate.metamodel.binding.ComponentAttributeBinding; import org.hibernate.metamodel.binding.EntityBinding; -import org.hibernate.metamodel.domain.Component; -import org.hibernate.metamodel.domain.SingularAttribute; -import org.hibernate.testing.FailureExpected; +import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; /** - * Tests for {@code j.p.Embeddable}. + * Tests for {@code javax.persistence.Embeddable}. * * @author Hardy Ferentschik */ -@FailureExpected(jiraKey = "HHH-6447", message = "Work in progress") + public class EmbeddableBindingTest extends BaseAnnotationBindingTestCase { @Test - //@Resources(annotatedClasses = { User.class, Address.class }) + @Resources(annotatedClasses = { User.class, Address.class }) public void testEmbeddable() { EntityBinding binding = getEntityBinding( User.class ); - assertNotNull( binding.locateAttributeBinding( "street" ) ); - assertNotNull( binding.locateAttributeBinding( "city" ) ); - assertNotNull( binding.locateAttributeBinding( "postCode" ) ); - SingularAttribute attribute = (SingularAttribute) binding.getEntity().locateAttribute( "address" ); - assertTrue( - "Wrong container type. Should be a component", - attribute.getSingularAttributeType() instanceof Component + assertNotNull( binding.locateAttributeBinding( "address" ) ); + assertTrue( binding.locateAttributeBinding( "address" ) instanceof ComponentAttributeBinding ); + ComponentAttributeBinding componentBinding = (ComponentAttributeBinding) binding.locateAttributeBinding( + "address" ); + + // todo - is this really correct? Does the path start w/ the class name + assertEquals( + "Wrong path", + "org.hibernate.metamodel.source.annotations.entity.EmbeddableBindingTest$User.address", + componentBinding.getPathBase() + ); + + assertNotNull( componentBinding.locateAttributeBinding( "street" ) ); + assertNotNull( componentBinding.locateAttributeBinding( "city" ) ); + assertNotNull( componentBinding.locateAttributeBinding( "postCode" ) ); } @Entity