Search in sources :

Example 21 with Column

use of javax.persistence.Column in project cosmic by MissionCriticalCloud.

the class Attribute method setupColumnInfo.

protected void setupColumnInfo(final Class<?> clazz, final AttributeOverride[] overrides, final String tableName, final boolean isEmbedded, final boolean isId) {
    flags = Flag.Selectable.setTrue(flags);
    final 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 {
        final 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);
        }
        final Encrypt encrypt = field.getAnnotation(Encrypt.class);
        if (encrypt != null && encrypt.encrypt()) {
            flags = Flag.Encrypted.setTrue(flags);
        }
    }
    final ElementCollection ec = field.getAnnotation(ElementCollection.class);
    if (ec != null) {
        flags = Flag.Insertable.setFalse(flags);
        flags = Flag.Selectable.setFalse(flags);
    }
    final 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 22 with Column

use of javax.persistence.Column in project mybatis.flying by limeng32.

the class SqlBuilder method buildConditionMapper.

private static void buildConditionMapper(ConditionMapper conditionMapper, ConditionMapperAnnotation conditionMapperAnnotation, Class<?> pojoClass, Field field) {
    conditionMapper.setFieldName(field.getName());
    conditionMapper.setDbFieldName(conditionMapperAnnotation.dbFieldName());
    conditionMapper.setConditionType(conditionMapperAnnotation.conditionType());
    conditionMapper.setSubTarget(conditionMapperAnnotation.subTarget());
    conditionMapper.setTypeHandlerPath(conditionMapperAnnotation.customTypeHandler());
    for (Field pojoField : pojoClass.getDeclaredFields()) {
        for (Annotation oan : pojoField.getDeclaredAnnotations()) {
            boolean b1 = oan instanceof FieldMapperAnnotation && ((FieldMapperAnnotation) oan).dbFieldName().equalsIgnoreCase(conditionMapperAnnotation.dbFieldName());
            boolean b2 = oan instanceof Column && (FieldMapper.getColumnName((Column) oan, pojoField)).equalsIgnoreCase(conditionMapperAnnotation.dbFieldName());
            boolean b3 = (conditionMapper.getSubTarget() != null) && (!Void.class.equals(conditionMapper.getSubTarget()));
            if (b1 || b2 || b3) {
                FieldMapper fieldMapper = new FieldMapper();
                if (b3) {
                    if (!tableMapperCache.containsKey(conditionMapper.getSubTarget())) {
                        buildTableMapper(conditionMapper.getSubTarget());
                    }
                    TableMapper tableMapper = tableMapperCache.get(conditionMapper.getSubTarget());
                    Map<String, FieldMapper> fieldMapperCache = tableMapper.getFieldMapperCache();
                    for (Map.Entry<String, FieldMapper> e : fieldMapperCache.entrySet()) {
                        if (conditionMapper.getDbFieldName().equalsIgnoreCase(e.getValue().getDbFieldName())) {
                            fieldMapper = e.getValue();
                            break;
                        }
                    }
                } else {
                    fieldMapper = new FieldMapper();
                    fieldMapper.buildMapper(pojoField);
                }
                conditionMapper.setFieldType(fieldMapper.getFieldType());
                conditionMapper.setJdbcType(fieldMapper.getJdbcType());
                conditionMapper.setCryptKeyColumn(fieldMapper.getCryptKeyColumn());
                conditionMapper.setCryptKeyAddition(fieldMapper.getCryptKeyAddition());
                conditionMapper.setDbFieldNameForJoin(fieldMapper.getDbFieldNameForJoin());
                if (!"".equals(fieldMapper.getDbAssociationUniqueKey())) {
                    conditionMapper.setDbAssociationUniqueKey(fieldMapper.getDbAssociationUniqueKey());
                    conditionMapper.setForeignKey(true);
                }
                if (conditionMapper.isForeignKey() && (!ConditionType.NULL_OR_NOT.equals(conditionMapper.getConditionType())) && !fieldMapper.isDelegate()) {
                    if (!tableMapperCache.containsKey(pojoField.getType())) {
                        buildTableMapper(pojoField.getType());
                    }
                    TableMapper tm = tableMapperCache.get(pojoField.getType());
                    String foreignFieldName = getFieldMapperByDbFieldName(tm.getFieldMapperCache(), fieldMapper.getDbAssociationUniqueKey()).getFieldName();
                    conditionMapper.setForeignFieldName(foreignFieldName);
                }
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Column(javax.persistence.Column) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) TableMapper(indi.mybatis.flying.models.TableMapper) FieldMapper(indi.mybatis.flying.models.FieldMapper) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation) TableMapperAnnotation(indi.mybatis.flying.annotations.TableMapperAnnotation) ConditionMapperAnnotation(indi.mybatis.flying.annotations.ConditionMapperAnnotation)

Example 23 with Column

use of javax.persistence.Column in project mybatis.flying by limeng32.

the class FieldMapper method buildMapper.

public boolean buildMapper(Field field) {
    Annotation[] fieldAnnotations = field.getDeclaredAnnotations();
    if (fieldAnnotations.length == 0) {
        return false;
    }
    boolean b = false;
    for (Annotation an1 : fieldAnnotations) {
        if (an1 instanceof Id) {
            setId((Id) an1);
        }
        if ((an1 instanceof FieldMapperAnnotation) || (an1 instanceof Column)) {
            b = true;
            setField(field);
            if (an1 instanceof FieldMapperAnnotation) {
                setFieldMapperAnnotation((FieldMapperAnnotation) an1);
            } else if (an1 instanceof Column) {
                setColumn((Column) an1);
            }
        }
    }
    if (b) {
        buildMapper();
    }
    return b;
}
Also used : Column(javax.persistence.Column) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Id(javax.persistence.Id) FieldMapperAnnotation(indi.mybatis.flying.annotations.FieldMapperAnnotation) Annotation(java.lang.annotation.Annotation)

Example 24 with Column

use of javax.persistence.Column in project dal by ctripcorp.

the class EntityManager method processAllFields.

private void processAllFields(String currentClassName, Field[] allFields, Map<String, String> columnNamesMap, Set<String> classNameSet) throws SQLException {
    for (Field field : allFields) {
        Column column = field.getAnnotation(Column.class);
        Id id = field.getAnnotation(Id.class);
        if (column == null && id == null)
            continue;
        if (field.getAnnotation(Type.class) == null)
            throw new DalException(ErrorCode.TypeNotDefined);
        String columnName = (column == null || column.name().trim().length() == 0) ? field.getName() : column.name();
        String tempClassName = columnNamesMap.get(columnName);
        if (tempClassName != null) {
            if (tempClassName.equals(currentClassName)) {
                // If two fields with same column name are in
                throw new DalException(ErrorCode.DuplicateColumnName);
            // same class,then we throw an exception.
            } else {
                // If two fields with same column name are distributed in different class,then we abandom
                continue;
            // current field.
            }
        }
        columnNamesMap.put(columnName, currentClassName);
        processField(columnName, field);
        processUpdatableColumn(columnName, column);
        processInsertableColumn(columnName, column);
        processPrimaryKeyColumn(id, columnName);
        processAutoIncrementColumn(field, currentClassName, classNameSet);
        processSensitiveColumn(columnName, field);
        processVersionColumn(columnName, field, currentClassName, columnNamesMap);
    }
}
Also used : Field(java.lang.reflect.Field) GenerationType(javax.persistence.GenerationType) Type(com.ctrip.platform.dal.dao.annotation.Type) Column(javax.persistence.Column) DalException(com.ctrip.platform.dal.exceptions.DalException) Id(javax.persistence.Id)

Example 25 with Column

use of javax.persistence.Column in project eweb4j-framework by laiweiwei.

the class PojoAnnotationConfig method getProperties.

private static List<Property> getProperties(Class<?> clazz, final List<Property> pList, final boolean requireSuper, Log log) throws Throwable {
    List<Property> result = new ArrayList<Property>();
    ReflectUtil ru;
    try {
        ru = new ReflectUtil(clazz);
        ru.setRequiredSuper(requireSuper);
    } catch (Throwable e) {
        log.warn(e.toString(), e);
        throw e;
    }
    for (Field f : ru.getFields()) {
        if (Collection.class.isAssignableFrom(f.getType()))
            continue;
        String name = f.getName();
        Method getter = ru.getGetter(name);
        if (getter == null)
            continue;
        Ignore igAnn = f.getAnnotation(Ignore.class);
        if (igAnn == null)
            igAnn = getter.getAnnotation(Ignore.class);
        if (igAnn != null)
            continue;
        Transient trans = f.getAnnotation(Transient.class);
        if (trans == null)
            trans = getter.getAnnotation(Transient.class);
        if (trans != null)
            continue;
        OneToMany manyAnn = getter.getAnnotation(OneToMany.class);
        if (manyAnn != null)
            continue;
        else {
            manyAnn = f.getAnnotation(OneToMany.class);
            if (manyAnn != null)
                continue;
        }
        ManyToMany manyManyAnn = getter.getAnnotation(ManyToMany.class);
        if (manyManyAnn != null)
            continue;
        else {
            manyManyAnn = f.getAnnotation(ManyToMany.class);
            if (manyManyAnn != null)
                continue;
        }
        Property p = new Property();
        if (Long.class.isAssignableFrom(f.getType()) || long.class.isAssignableFrom(f.getType()))
            p.setSize("20");
        else if (Integer.class.isAssignableFrom(f.getType()) || int.class.isAssignableFrom(f.getType()))
            p.setSize("8");
        else if (String.class.isAssignableFrom(f.getType()))
            p.setSize("255");
        else if (Boolean.class.isAssignableFrom(f.getType()) || boolean.class.isAssignableFrom(f.getType()))
            p.setSize("");
        else if (Float.class.isAssignableFrom(f.getType()) || float.class.isAssignableFrom(f.getType()))
            p.setSize("8");
        Id idAnn = getter.getAnnotation(Id.class);
        if (idAnn == null)
            idAnn = f.getAnnotation(Id.class);
        if (idAnn != null) {
            if (pList != null && hasIdProperty(pList))
                continue;
            p.setAutoIncrement("1");
            p.setPk("1");
            p.setSize("20");
        }
        Column colAnn = getter.getAnnotation(Column.class);
        if (colAnn == null) {
            colAnn = f.getAnnotation(Column.class);
        }
        String column = colAnn == null ? "" : colAnn.name();
        column = "".equals(column.trim()) ? name : column;
        p.setName(name);
        p.setColumn(column);
        p.setType(f.getType().getName());
        p.setNotNull("false");
        if (colAnn != null) {
            // int size = colAnn.length();
            p.setNotNull(String.valueOf(!colAnn.nullable()));
            p.setUnique(String.valueOf(colAnn.unique()));
        }
        if (ClassUtil.isPojo(f.getType())) {
            OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
            if (oneAnn == null)
                oneAnn = f.getAnnotation(OneToOne.class);
            ManyToOne manyToOneAnn = null;
            if (oneAnn == null) {
                manyToOneAnn = getter.getAnnotation(ManyToOne.class);
                if (manyToOneAnn == null)
                    manyToOneAnn = f.getAnnotation(ManyToOne.class);
            }
            if (oneAnn != null || manyToOneAnn != null) {
                if (oneAnn != null)
                    p.setType(PropType.ONE_ONE);
                else
                    p.setType(PropType.MANY_ONE);
                JoinColumn joinColumn = getter.getAnnotation(JoinColumn.class);
                if (joinColumn == null)
                    joinColumn = f.getAnnotation(JoinColumn.class);
                if (joinColumn != null && joinColumn.name().trim().length() > 0)
                    p.setColumn(joinColumn.name());
                else
                    p.setColumn(f.getName() + "_id");
                p.setRelProperty(null);
                String refCol = null;
                if (joinColumn != null && joinColumn.referencedColumnName().trim().length() > 0) {
                    refCol = joinColumn.referencedColumnName();
                    if (refCol != null && refCol.trim().length() > 0) {
                        String relField = ORMConfigBeanUtil.getField(f.getType(), refCol);
                        if (relField != null && relField.trim().length() > 0)
                            p.setRelProperty(relField);
                    }
                }
                p.setRelClass(f.getType());
                p.setSize("20");
            }
        }
        result.add(p);
    }
    return result;
}
Also used : Ignore(org.eweb4j.orm.annotation.Ignore) ArrayList(java.util.ArrayList) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) OneToMany(javax.persistence.OneToMany) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) Transient(javax.persistence.Transient) Id(javax.persistence.Id) Property(org.eweb4j.orm.config.bean.Property)

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