Search in sources :

Example 1 with AttributeOverride

use of javax.persistence.AttributeOverride in project hibernate-orm by hibernate.

the class AbstractPropertyHolder method buildColumnOverride.

private static Map<String, Column[]> buildColumnOverride(XAnnotatedElement element, String path) {
    Map<String, Column[]> columnOverride = new HashMap<String, Column[]>();
    if (element != null) {
        AttributeOverride singleOverride = element.getAnnotation(AttributeOverride.class);
        AttributeOverrides multipleOverrides = element.getAnnotation(AttributeOverrides.class);
        AttributeOverride[] overrides;
        if (singleOverride != null) {
            overrides = new AttributeOverride[] { singleOverride };
        } else if (multipleOverrides != null) {
            overrides = multipleOverrides.value();
        } else {
            overrides = null;
        }
        if (overrides != null) {
            for (AttributeOverride depAttr : overrides) {
                columnOverride.put(StringHelper.qualify(path, depAttr.name()), new Column[] { depAttr.column() });
            }
        }
    }
    return columnOverride;
}
Also used : AttributeOverrides(javax.persistence.AttributeOverrides) HashMap(java.util.HashMap) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) AttributeOverride(javax.persistence.AttributeOverride)

Example 2 with AttributeOverride

use of javax.persistence.AttributeOverride in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method mergeAttributeOverrides.

/**
	 * @param mergeWithAnnotations Whether to use Java annotations for this
	 * element, if present and not disabled by the XMLContext defaults.
	 * In some contexts (such as an association mapping) merging with
	 * annotations is never allowed.
	 */
private AttributeOverrides mergeAttributeOverrides(XMLContext.Default defaults, List<AttributeOverride> attributes, boolean mergeWithAnnotations) {
    if (mergeWithAnnotations && defaults.canUseJavaAnnotations()) {
        AttributeOverride annotation = getPhysicalAnnotation(AttributeOverride.class);
        addAttributeOverrideIfNeeded(annotation, attributes);
        AttributeOverrides annotations = getPhysicalAnnotation(AttributeOverrides.class);
        if (annotations != null) {
            for (AttributeOverride current : annotations.value()) {
                addAttributeOverrideIfNeeded(current, attributes);
            }
        }
    }
    if (attributes.size() > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(AttributeOverrides.class);
        ad.setValue("value", attributes.toArray(new AttributeOverride[attributes.size()]));
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AttributeOverrides(javax.persistence.AttributeOverrides) AttributeOverride(javax.persistence.AttributeOverride)

Example 3 with AttributeOverride

use of javax.persistence.AttributeOverride in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildAttributeOverrides.

private List<AttributeOverride> buildAttributeOverrides(List<Element> subelements, String nodeName) {
    List<AttributeOverride> overrides = new ArrayList<AttributeOverride>();
    if (subelements != null && subelements.size() > 0) {
        for (Element current : subelements) {
            if (!current.getName().equals(nodeName)) {
                continue;
            }
            AnnotationDescriptor override = new AnnotationDescriptor(AttributeOverride.class);
            copyStringAttribute(override, current, "name", true);
            Element column = current.element("column");
            override.setValue("column", getColumn(column, true, current));
            overrides.add((AttributeOverride) AnnotationFactory.create(override));
        }
    }
    return overrides;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AttributeOverride(javax.persistence.AttributeOverride)

Example 4 with AttributeOverride

use of javax.persistence.AttributeOverride in project hibernate-orm by hibernate.

the class Ejb3XmlElementCollectionTest method testSingleMapKeyAttributeOverride.

/**
	 * When there's a single map key attribute override, we still wrap it with
	 * an AttributeOverrides annotation.
	 */
@Test
public void testSingleMapKeyAttributeOverride() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm10.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(MapKey.class);
    assertAnnotationNotPresent(MapKeyClass.class);
    assertAnnotationNotPresent(MapKeyTemporal.class);
    assertAnnotationNotPresent(MapKeyEnumerated.class);
    assertAnnotationNotPresent(MapKeyColumn.class);
    assertAnnotationNotPresent(MapKeyJoinColumns.class);
    assertAnnotationNotPresent(MapKeyJoinColumn.class);
    assertAnnotationNotPresent(AttributeOverride.class);
    assertAnnotationPresent(AttributeOverrides.class);
    AttributeOverrides overridesAnno = reader.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] overrides = overridesAnno.value();
    assertEquals(1, overrides.length);
    assertEquals("field1", overrides[0].name());
    assertEquals("col1", overrides[0].column().name());
}
Also used : AttributeOverrides(javax.persistence.AttributeOverrides) AttributeOverride(javax.persistence.AttributeOverride) Test(org.junit.Test)

Example 5 with AttributeOverride

use of javax.persistence.AttributeOverride in project hibernate-orm by hibernate.

the class Ejb3XmlElementCollectionTest method testMixedAttributeOverrides.

/**
	 * Tests that map-key-attribute-override and attribute-override elements
	 * both end up in the AttributeOverrides annotation.
	 */
@Test
public void testMixedAttributeOverrides() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm23.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(AttributeOverride.class);
    assertAnnotationPresent(AttributeOverrides.class);
    AttributeOverrides overridesAnno = reader.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] overrides = overridesAnno.value();
    assertEquals(2, overrides.length);
    assertEquals("field1", overrides[0].name());
    assertEquals("col1", overrides[0].column().name());
    assertEquals("field2", overrides[1].name());
    assertEquals("col2", overrides[1].column().name());
}
Also used : AttributeOverrides(javax.persistence.AttributeOverrides) AttributeOverride(javax.persistence.AttributeOverride) Test(org.junit.Test)

Aggregations

AttributeOverride (javax.persistence.AttributeOverride)18 AttributeOverrides (javax.persistence.AttributeOverrides)13 Test (org.junit.Test)9 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)3 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 EmbeddedId (javax.persistence.EmbeddedId)2 Element (org.dom4j.Element)2 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 Column (javax.persistence.Column)1 JoinColumn (javax.persistence.JoinColumn)1