Search in sources :

Example 1 with AttributeOverrides

use of javax.persistence.AttributeOverrides in project CloudStack-archive by CloudStack-extras.

the class DbUtil method getAttributeOverrides.

public static final AttributeOverride[] getAttributeOverrides(AnnotatedElement ae) {
    AttributeOverride[] overrides = null;
    AttributeOverrides aos = ae.getAnnotation(AttributeOverrides.class);
    if (aos != null) {
        overrides = aos.value();
    }
    if (overrides == null || overrides.length == 0) {
        AttributeOverride override = ae.getAnnotation(AttributeOverride.class);
        if (override != null) {
            overrides = new AttributeOverride[1];
            overrides[0] = override;
        } else {
            overrides = new AttributeOverride[0];
        }
    }
    return overrides;
}
Also used : AttributeOverrides(javax.persistence.AttributeOverrides) AttributeOverride(javax.persistence.AttributeOverride)

Example 2 with AttributeOverrides

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

the class Ejb3XmlManyToManyTest method testMultipleMapKeyAttributeOverrides.

@Test
public void testMultipleMapKeyAttributeOverrides() throws Exception {
    reader = getReader(Entity3.class, "field1", "many-to-many.orm11.xml");
    assertAnnotationPresent(ManyToMany.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(javax.persistence.AttributeOverrides) AttributeOverride(javax.persistence.AttributeOverride) Test(org.junit.Test)

Example 3 with AttributeOverrides

use of javax.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<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) {
            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(javax.persistence.AttributeOverrides) HashMap(java.util.HashMap) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) AttributeOverride(javax.persistence.AttributeOverride)

Example 4 with AttributeOverrides

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

Example 5 with AttributeOverrides

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

Aggregations

AttributeOverride (javax.persistence.AttributeOverride)14 AttributeOverrides (javax.persistence.AttributeOverrides)14 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Column (javax.persistence.Column)1 JoinColumn (javax.persistence.JoinColumn)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1