Search in sources :

Example 1 with AttributeOverrides

use of jakarta.persistence.AttributeOverrides 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 AttributeOverrides

use of jakarta.persistence.AttributeOverrides 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 3 with AttributeOverrides

use of jakarta.persistence.AttributeOverrides 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)

Example 4 with AttributeOverrides

use of jakarta.persistence.AttributeOverrides 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(jakarta.persistence.AttributeOverrides) AttributeOverride(jakarta.persistence.AttributeOverride) Test(org.junit.Test)

Example 5 with AttributeOverrides

use of jakarta.persistence.AttributeOverrides 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(jakarta.persistence.AttributeOverrides) AttributeOverride(jakarta.persistence.AttributeOverride) Test(org.junit.Test)

Aggregations

AttributeOverride (jakarta.persistence.AttributeOverride)11 AttributeOverrides (jakarta.persistence.AttributeOverrides)11 Test (org.junit.Test)9 Column (jakarta.persistence.Column)1 JoinColumn (jakarta.persistence.JoinColumn)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 JaxbAttributeOverride (org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride)1