From b336bf5d539e0758a2147f8b0f1b249f783cc8f7 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 2 Aug 2011 12:40:02 +0200 Subject: [PATCH] HHH-6501 Adding support for @Parent in EmbeddedClass --- .../source/annotations/JandexHelper.java | 16 --------- .../entity/ComponentAttributeSourceImpl.java | 14 +++----- .../annotations/entity/EmbeddableClass.java | 22 +++++++++++++ .../entity/EmbeddableBindingTest.java | 33 +++++++++++++++++++ 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java index dfde432367..8a8dd67811 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/JandexHelper.java @@ -274,22 +274,6 @@ public class JandexHelper { return annotations; } - public static Map> getTypeAnnotations(ClassInfo classInfo) { - if ( classInfo == null ) { - throw new IllegalArgumentException( "classInfo cannot be null" ); - } - - Map> annotations = new HashMap>(); - for ( List annotationList : classInfo.annotations().values() ) { - for ( AnnotationInstance instance : annotationList ) { - if ( instance.target() instanceof ClassInfo ) { - addAnnotationToMap( instance, annotations ); - } - } - } - return annotations; - } - public static void addAnnotationToMap(AnnotationInstance instance, Map> annotations) { DotName dotName = instance.name(); List list; 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 561b3298e4..57d17422ae 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 @@ -29,13 +29,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.jboss.jandex.AnnotationInstance; - import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.Value; import org.hibernate.mapping.PropertyGeneration; import org.hibernate.metamodel.source.LocalBindingContext; -import org.hibernate.metamodel.source.annotations.JandexHelper; import org.hibernate.metamodel.source.annotations.attribute.AssociationAttribute; import org.hibernate.metamodel.source.annotations.attribute.AttributeOverride; import org.hibernate.metamodel.source.annotations.attribute.BasicAttribute; @@ -149,6 +146,11 @@ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { return path; } + @Override + public String getParentReferenceAttributeName() { + return embeddableClass.getParentReferencingAttributeName(); + } + @Override public Iterable metaAttributes() { // not relevant for annotations @@ -161,12 +163,6 @@ public class ComponentAttributeSourceImpl implements ComponentAttributeSource { return null; } - @Override - public String getParentReferenceAttributeName() { - // see HHH-6501 - return null; - } - @Override public ExplicitHibernateTypeSource getTypeInformation() { // probably need to check for @Target in EmbeddableClass (HF) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableClass.java index e29f92497e..f1fa105423 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EmbeddableClass.java @@ -25,9 +25,12 @@ package org.hibernate.metamodel.source.annotations.entity; import javax.persistence.AccessType; +import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.ClassInfo; import org.hibernate.metamodel.source.annotations.AnnotationBindingContext; +import org.hibernate.metamodel.source.annotations.HibernateDotNames; +import org.hibernate.metamodel.source.annotations.JandexHelper; /** * Represents the information about an entity annotated with {@code @Embeddable}. @@ -36,6 +39,7 @@ import org.hibernate.metamodel.source.annotations.AnnotationBindingContext; */ public class EmbeddableClass extends ConfiguredClass { private final String embeddedAttributeName; + private final String parentReferencingAttributeName; public EmbeddableClass( ClassInfo classInfo, @@ -45,11 +49,29 @@ public class EmbeddableClass extends ConfiguredClass { AnnotationBindingContext context) { super( classInfo, defaultAccessType, parent, context ); this.embeddedAttributeName = embeddedAttributeName; + this.parentReferencingAttributeName = checkParentAnnotation(); + } + + private String checkParentAnnotation() { + AnnotationInstance parentAnnotation = JandexHelper.getSingleAnnotation( + getClassInfo(), + HibernateDotNames.PARENT + ); + if ( parentAnnotation == null ) { + return null; + } + else { + return JandexHelper.getPropertyName( parentAnnotation.target() ); + } } public String getEmbeddedAttributeName() { return embeddedAttributeName; } + + public String getParentReferencingAttributeName() { + return parentReferencingAttributeName; + } } 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 d90ad4d0fb..88464c2d05 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 @@ -33,6 +33,7 @@ import javax.persistence.Id; import org.junit.Test; +import org.hibernate.annotations.Parent; import org.hibernate.metamodel.binding.BasicAttributeBinding; import org.hibernate.metamodel.binding.ComponentAttributeBinding; import org.hibernate.metamodel.binding.EntityBinding; @@ -255,6 +256,38 @@ public class EmbeddableBindingTest extends BaseAnnotationBindingTestCase { column.getColumnName().getName() ); } + + + @Embeddable + public class EmbeddableEntity { + private String test; + @Parent + private MainEntity parent; + } + + @Entity + public class MainEntity { + @Id + private int id; + + @Embedded + private EmbeddableEntity embedded; + } + + @Test + @Resources(annotatedClasses = { MainEntity.class, EmbeddableEntity.class }) + public void testParentReferencingAttributeName() { + EntityBinding binding = getEntityBinding( MainEntity.class ); + + final String componentName = "embedded"; + assertNotNull( binding.locateAttributeBinding( componentName ) ); + assertTrue( binding.locateAttributeBinding( componentName ) instanceof ComponentAttributeBinding ); + ComponentAttributeBinding componentBinding = (ComponentAttributeBinding) binding.locateAttributeBinding( + componentName + ); + + assertEquals( "Wrong parent reference name", "parent", componentBinding.getParentReference().getName() ); + } }