Search in sources :

Example 26 with Column

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

the class Ejb3XmlElementCollectionTest method testColumnAllAttributes.

@Test
public void testColumnAllAttributes() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm17.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationPresent(Column.class);
    Column column = reader.getAnnotation(Column.class);
    assertEquals("col1", column.name());
    assertTrue(column.unique());
    assertFalse(column.nullable());
    assertFalse(column.insertable());
    assertFalse(column.updatable());
    assertEquals("int", column.columnDefinition());
    assertEquals("table1", column.table());
    assertEquals(50, column.length());
    assertEquals(2, column.precision());
    assertEquals(1, column.scale());
}
Also used : MapKeyColumn(javax.persistence.MapKeyColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) OrderColumn(javax.persistence.OrderColumn) Test(org.junit.Test)

Example 27 with Column

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

the class GenericDaoBase method prepareAttribute.

@DB(txn = false)
protected void prepareAttribute(final int j, final PreparedStatement pstmt, final Attribute attr, Object value) throws SQLException {
    if (attr.is(Attribute.Flag.DaoGenerated) && value == null) {
        value = generateValue(attr);
        if (attr.field == null) {
            pstmt.setObject(j, value);
            return;
        }
    }
    if (attr.field.getType() == String.class) {
        final String str = (String) value;
        if (str == null) {
            pstmt.setString(j, null);
            return;
        }
        final Column column = attr.field.getAnnotation(Column.class);
        final int length = column != null ? column.length() : 255;
        // to support generic localization, utilize MySql UTF-8 support
        if (length < str.length()) {
            try {
                if (attr.is(Attribute.Flag.Encrypted)) {
                    pstmt.setBytes(j, DBEncryptionUtil.encrypt(str.substring(0, column.length())).getBytes("UTF-8"));
                } else {
                    pstmt.setBytes(j, str.substring(0, column.length()).getBytes("UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                // no-way it can't support UTF-8 encoding
                assert (false);
                throw new CloudRuntimeException("UnsupportedEncodingException when saving string as UTF-8 data");
            }
        } else {
            try {
                if (attr.is(Attribute.Flag.Encrypted)) {
                    pstmt.setBytes(j, DBEncryptionUtil.encrypt(str).getBytes("UTF-8"));
                } else {
                    pstmt.setBytes(j, str.getBytes("UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                // no-way it can't support UTF-8 encoding
                assert (false);
                throw new CloudRuntimeException("UnsupportedEncodingException when saving string as UTF-8 data");
            }
        }
    } else if (attr.field.getType() == Date.class) {
        final Date date = (Date) value;
        if (date == null) {
            pstmt.setObject(j, null);
            return;
        }
        if (attr.is(Attribute.Flag.Date)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
        } else if (attr.is(Attribute.Flag.TimeStamp)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
        } else if (attr.is(Attribute.Flag.Time)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, date));
        }
    } else if (attr.field.getType() == Calendar.class) {
        final Calendar cal = (Calendar) value;
        if (cal == null) {
            pstmt.setObject(j, null);
            return;
        }
        if (attr.is(Attribute.Flag.Date)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
        } else if (attr.is(Attribute.Flag.TimeStamp)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
        } else if (attr.is(Attribute.Flag.Time)) {
            pstmt.setString(j, DateUtil.getDateDisplayString(s_gmtTimeZone, cal.getTime()));
        }
    } else if (attr.field.getType().isEnum()) {
        final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
        final EnumType type = (enumerated == null) ? EnumType.STRING : enumerated.value();
        if (type == EnumType.STRING) {
            pstmt.setString(j, value == null ? null : value.toString());
        } else if (type == EnumType.ORDINAL) {
            pstmt.setInt(j, value == null ? null : ((Enum<?>) value).ordinal());
        }
    } else if (attr.field.getType() == URI.class) {
        pstmt.setString(j, value == null ? null : value.toString());
    } else if (attr.field.getType() == URL.class) {
        pstmt.setURL(j, (URL) value);
    } else if (attr.field.getType() == byte[].class) {
        pstmt.setBytes(j, (byte[]) value);
    } else if (attr.field.getType() == Ip.class) {
        final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
        final EnumType type = (enumerated == null) ? EnumType.ORDINAL : enumerated.value();
        if (type == EnumType.STRING) {
            pstmt.setString(j, value == null ? null : value.toString());
        } else if (type == EnumType.ORDINAL) {
            pstmt.setLong(j, value == null ? null : (value instanceof Ip) ? ((Ip) value).longValue() : NetUtils.ip2Long((String) value));
        }
    } else {
        pstmt.setObject(j, value);
    }
}
Also used : Enumerated(javax.persistence.Enumerated) Column(javax.persistence.Column) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) EnumType(javax.persistence.EnumType) Calendar(java.util.Calendar) Ip(com.cloud.utils.net.Ip) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) URL(java.net.URL)

Example 28 with Column

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

the class Attribute method setupColumnInfo.

protected void setupColumnInfo(Class<?> clazz, AttributeOverride[] overrides, String tableName, boolean isEmbedded, boolean isId) {
    flags = Flag.Selectable.setTrue(flags);
    GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
    if (gv != null) {
        if (gv.strategy() == GenerationType.IDENTITY) {
            flags = Flag.DbGenerated.setTrue(flags);
        } else if (gv.strategy() == GenerationType.SEQUENCE) {
            assert (false) : "Sequence generation not supported.";
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.SequenceGV.setTrue(flags);
        } else if (gv.strategy() == GenerationType.TABLE) {
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.TableGV.setTrue(flags);
        } else if (gv.strategy() == GenerationType.AUTO) {
            flags = Flag.DaoGenerated.setTrue(flags);
            flags = Flag.Insertable.setTrue(flags);
            flags = Flag.AutoGV.setTrue(flags);
        }
    }
    if (isEmbedded) {
        flags = Flag.Embedded.setTrue(flags);
    }
    if (isId) {
        flags = Flag.Id.setTrue(flags);
    } else {
        Id id = field.getAnnotation(Id.class);
        if (id != null) {
            flags = Flag.Id.setTrue(flags);
        }
    }
    column = field.getAnnotation(Column.class);
    if (gv == null) {
        if (column == null || (column.insertable() && column.table().length() == 0)) {
            flags = Flag.Insertable.setTrue(flags);
        }
        if (column == null || (column.updatable() && column.table().length() == 0)) {
            flags = Flag.Updatable.setTrue(flags);
        }
        if (column == null || column.nullable()) {
            flags = Flag.Nullable.setTrue(flags);
        }
        if (column != null && column.encryptable()) {
            flags = Flag.Encrypted.setTrue(flags);
        }
    }
    ElementCollection ec = field.getAnnotation(ElementCollection.class);
    if (ec != null) {
        flags = Flag.Insertable.setFalse(flags);
        flags = Flag.Selectable.setFalse(flags);
    }
    Temporal temporal = field.getAnnotation(Temporal.class);
    if (temporal != null) {
        if (temporal.value() == TemporalType.DATE) {
            flags = Flag.Date.setTrue(flags);
        } else if (temporal.value() == TemporalType.TIME) {
            flags = Flag.Time.setTrue(flags);
        } else if (temporal.value() == TemporalType.TIMESTAMP) {
            flags = Flag.TimeStamp.setTrue(flags);
        }
    }
    if (column != null && column.table().length() > 0) {
        table = column.table();
    }
    columnName = DbUtil.getColumnName(field, overrides);
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) Temporal(javax.persistence.Temporal) Column(javax.persistence.Column) Id(javax.persistence.Id) ElementCollection(javax.persistence.ElementCollection)

Example 29 with Column

use of javax.persistence.Column in project bamboobsc by billchen198318.

the class SqlGenerateUtil method getField.

private static Map<String, Object> getField(Object entityObject) throws Exception {
    Map<String, Object> fieldMap = new HashMap<String, Object>();
    Method[] methods = entityObject.getClass().getMethods();
    for (int ix = 0; ix < methods.length; ix++) {
        Annotation[] annotations = methods[ix].getDeclaredAnnotations();
        if (annotations == null) {
            continue;
        }
        for (Annotation annotation : annotations) {
            if (annotation instanceof Column) {
                if (methods[ix].getName().indexOf("get") != 0) {
                    continue;
                }
                String column = StringUtils.defaultString(((Column) annotation).name());
                if ("".equals(column.trim())) {
                    continue;
                }
                Object value = methods[ix].invoke(entityObject);
                fieldMap.put(column, value);
            }
        }
    }
    return fieldMap;
}
Also used : HashMap(java.util.HashMap) Column(javax.persistence.Column) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 30 with Column

use of javax.persistence.Column in project OpenOLAT by OpenOLAT.

the class AbstractUserPropertyHandler method setInternalGetterSetter.

protected void setInternalGetterSetter(String name) {
    try {
        Field getter = UserImpl.class.getDeclaredField(name);
        getter.setAccessible(true);
        if (getter.isAnnotationPresent(Column.class)) {
            Column col = getter.getAnnotation(Column.class);
            databaseColumnName = col.name();
        }
    } catch (NoSuchFieldException | SecurityException e) {
        log.error("", e);
    }
}
Also used : Field(java.lang.reflect.Field) Column(javax.persistence.Column)

Aggregations

Column (javax.persistence.Column)45 Field (java.lang.reflect.Field)15 JoinColumn (javax.persistence.JoinColumn)15 Id (javax.persistence.Id)11 ArrayList (java.util.ArrayList)9 Method (java.lang.reflect.Method)8 HashMap (java.util.HashMap)8 MapKeyColumn (javax.persistence.MapKeyColumn)7 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)7 OrderColumn (javax.persistence.OrderColumn)7 Annotation (java.lang.annotation.Annotation)6 ManyToOne (javax.persistence.ManyToOne)6 Date (java.util.Date)4 Map (java.util.Map)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)4 ElementCollection (javax.persistence.ElementCollection)4 Enumerated (javax.persistence.Enumerated)4 GeneratedValue (javax.persistence.GeneratedValue)4 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)4 XProperty (org.hibernate.annotations.common.reflection.XProperty)4