HHH-6501 Adding support for @Parent in EmbeddedClass

This commit is contained in:
Hardy Ferentschik 2011-08-02 12:40:02 +02:00
parent 6fb38e45ae
commit b336bf5d53
4 changed files with 60 additions and 25 deletions

View File

@ -274,22 +274,6 @@ public class JandexHelper {
return annotations;
}
public static Map<DotName, List<AnnotationInstance>> getTypeAnnotations(ClassInfo classInfo) {
if ( classInfo == null ) {
throw new IllegalArgumentException( "classInfo cannot be null" );
}
Map<DotName, List<AnnotationInstance>> annotations = new HashMap<DotName, List<AnnotationInstance>>();
for ( List<AnnotationInstance> 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<DotName, List<AnnotationInstance>> annotations) {
DotName dotName = instance.name();
List<AnnotationInstance> list;

View File

@ -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<MetaAttributeSource> 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)

View File

@ -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;
}
}

View File

@ -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() );
}
}