Search in sources :

Example 1 with AttributeOverride

use of jakarta.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<>();
    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) {
            Map<String, List<Column>> columnOverrideList = new HashMap<>();
            for (AttributeOverride depAttr : overrides) {
                String qualifiedName = StringHelper.qualify(path, depAttr.name());
                if (columnOverrideList.containsKey(qualifiedName)) {
                    columnOverrideList.get(qualifiedName).add(depAttr.column());
                } else {
                    columnOverrideList.put(qualifiedName, new ArrayList<>(Arrays.asList(depAttr.column())));
                }
            }
            for (Map.Entry<String, List<Column>> entry : columnOverrideList.entrySet()) {
                columnOverride.put(entry.getKey(), entry.getValue().toArray(new Column[entry.getValue().size()]));
            }
        }
    }
    return columnOverride;
}
Also used : AttributeOverrides(jakarta.persistence.AttributeOverrides) HashMap(java.util.HashMap) JoinColumn(jakarta.persistence.JoinColumn) Column(jakarta.persistence.Column) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) AttributeOverride(jakarta.persistence.AttributeOverride)

Example 2 with AttributeOverride

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

the class JPAXMLOverriddenAnnotationReader method getElementCollection.

/**
 * As per sections 12.2.3.23.9, 12.2.4.8.9 and 12.2.5.3.6 of the JPA 2.0
 * specification, the element-collection subelement completely overrides the
 * mapping for the specified field or property.  Thus, any methods which
 * might in some contexts merge with annotations must not do so in this
 * context.
 */
private void getElementCollection(List<Annotation> annotationList, XMLContext.Default defaults) {
    for (JaxbElementCollection element : elementsForProperty.getElementCollection()) {
        AnnotationDescriptor ad = new AnnotationDescriptor(ElementCollection.class);
        addTargetClass(element.getTargetClass(), ad, "target-class", defaults);
        getFetchType(ad, element.getFetch());
        getOrderBy(annotationList, element.getOrderBy());
        getOrderColumn(annotationList, element.getOrderColumn());
        getMapKey(annotationList, element.getMapKey());
        getMapKeyClass(annotationList, element.getMapKeyClass(), defaults);
        getMapKeyTemporal(annotationList, element.getMapKeyTemporal());
        getMapKeyEnumerated(annotationList, element.getMapKeyEnumerated());
        getMapKeyColumn(annotationList, element.getMapKeyColumn());
        getMapKeyJoinColumns(annotationList, element.getMapKeyJoinColumn());
        Annotation annotation = getColumn(element.getColumn(), false, "element-collection");
        addIfNotNull(annotationList, annotation);
        getTemporal(annotationList, element.getTemporal());
        getEnumerated(annotationList, element.getEnumerated());
        getLob(annotationList, element.getLob());
        // Both map-key-attribute-overrides and attribute-overrides
        // translate into AttributeOverride annotations, which need
        // need to be wrapped in the same AttributeOverrides annotation.
        List<AttributeOverride> attributes = new ArrayList<>();
        attributes.addAll(buildAttributeOverrides(element.getMapKeyAttributeOverride(), "map-key-attribute-override"));
        attributes.addAll(buildAttributeOverrides(element.getAttributeOverride(), "attribute-override"));
        annotation = mergeAttributeOverrides(defaults, attributes, false);
        addIfNotNull(annotationList, annotation);
        annotation = getAssociationOverrides(element.getAssociationOverride(), defaults, false);
        addIfNotNull(annotationList, annotation);
        getCollectionTable(annotationList, element.getCollectionTable(), defaults);
        annotationList.add(AnnotationFactory.create(ad));
        getAccessType(annotationList, element.getAccess());
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ArrayList(java.util.ArrayList) JaxbElementCollection(org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollection) Annotation(java.lang.annotation.Annotation) JaxbAttributeOverride(org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride) AttributeOverride(jakarta.persistence.AttributeOverride)

Example 3 with AttributeOverride

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

the class JPAXMLOverriddenAnnotationReader 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(jakarta.persistence.AttributeOverrides) JaxbAttributeOverride(org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride) AttributeOverride(jakarta.persistence.AttributeOverride)

Example 4 with AttributeOverride

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

the class JPAXMLOverriddenAnnotationReader method addAttributeOverrideIfNeeded.

private void addAttributeOverrideIfNeeded(AttributeOverride annotation, List<AttributeOverride> overrides) {
    if (annotation != null) {
        String overrideName = annotation.name();
        boolean present = false;
        for (AttributeOverride current : overrides) {
            if (current.name().equals(overrideName)) {
                present = true;
                break;
            }
        }
        if (!present) {
            overrides.add(annotation);
        }
    }
}
Also used : JaxbAttributeOverride(org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride) AttributeOverride(jakarta.persistence.AttributeOverride)

Example 5 with AttributeOverride

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

the class Ejb3XmlElementCollectionTest method testMultipleMapKeyAttributeOverrides.

@Test
public void testMultipleMapKeyAttributeOverrides() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm11.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(2, overrides.length);
    assertEquals("field1", overrides[0].name());
    assertEquals("", overrides[0].column().name());
    assertFalse(overrides[0].column().unique());
    assertTrue(overrides[0].column().nullable());
    assertTrue(overrides[0].column().insertable());
    assertTrue(overrides[0].column().updatable());
    assertEquals("", overrides[0].column().columnDefinition());
    assertEquals("", overrides[0].column().table());
    assertEquals(255, overrides[0].column().length());
    assertEquals(0, overrides[0].column().precision());
    assertEquals(0, overrides[0].column().scale());
    assertEquals("field2", overrides[1].name());
    assertEquals("col1", overrides[1].column().name());
    assertTrue(overrides[1].column().unique());
    assertFalse(overrides[1].column().nullable());
    assertFalse(overrides[1].column().insertable());
    assertFalse(overrides[1].column().updatable());
    assertEquals("int", overrides[1].column().columnDefinition());
    assertEquals("table1", overrides[1].column().table());
    assertEquals(50, overrides[1].column().length());
    assertEquals(2, overrides[1].column().precision());
    assertEquals(1, overrides[1].column().scale());
}
Also used : AttributeOverrides(jakarta.persistence.AttributeOverrides) AttributeOverride(jakarta.persistence.AttributeOverride) Test(org.junit.Test)

Aggregations

AttributeOverride (jakarta.persistence.AttributeOverride)15 AttributeOverrides (jakarta.persistence.AttributeOverrides)12 Test (org.junit.Test)9 JaxbAttributeOverride (org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride)4 ArrayList (java.util.ArrayList)3 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)3 Access (jakarta.persistence.Access)1 Column (jakarta.persistence.Column)1 JoinColumn (jakarta.persistence.JoinColumn)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AssertionFailure (org.hibernate.AssertionFailure)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 JaxbColumn (org.hibernate.boot.jaxb.mapping.spi.JaxbColumn)1 JaxbElementCollection (org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollection)1 AccessType (org.hibernate.cfg.AccessType)1 AnnotatedClassType (org.hibernate.cfg.AnnotatedClassType)1