Search in sources :

Example 6 with AttributeOverride

use of javax.persistence.AttributeOverride 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 7 with AttributeOverride

use of javax.persistence.AttributeOverride 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 8 with AttributeOverride

use of javax.persistence.AttributeOverride in project cloudstack by apache.

the class GenericDaoBase method buildSelectByIdSql.

@DB()
protected String buildSelectByIdSql(final StringBuilder sql) {
    if (_idField == null) {
        return null;
    }
    if (_idField.getAnnotation(EmbeddedId.class) == null) {
        sql.append(_table).append(".").append(DbUtil.getColumnName(_idField, null)).append(" = ? ");
    } else {
        final Class<?> clazz = _idField.getClass();
        final AttributeOverride[] overrides = DbUtil.getAttributeOverrides(_idField);
        for (final Field field : clazz.getDeclaredFields()) {
            sql.append(_table).append(".").append(DbUtil.getColumnName(field, overrides)).append(" = ? AND ");
        }
        sql.delete(sql.length() - 4, sql.length());
    }
    return sql.toString();
}
Also used : Field(java.lang.reflect.Field) EmbeddedId(javax.persistence.EmbeddedId) AttributeOverride(javax.persistence.AttributeOverride)

Example 9 with AttributeOverride

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

the class Ejb3XmlElementCollectionTest method testMultipleAttributeOverrides.

@Test
public void testMultipleAttributeOverrides() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm22.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("", 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 10 with AttributeOverride

use of javax.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(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