Search in sources :

Example 1 with Column

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

the class RevisionInfoConfiguration method searchForRevisionInfoCfgInProperties.

private void searchForRevisionInfoCfgInProperties(XClass clazz, ReflectionManager reflectionManager, MutableBoolean revisionNumberFound, MutableBoolean revisionTimestampFound, MutableBoolean modifiedEntityNamesFound, String accessType) {
    for (XProperty property : clazz.getDeclaredProperties(accessType)) {
        final RevisionNumber revisionNumber = property.getAnnotation(RevisionNumber.class);
        final RevisionTimestamp revisionTimestamp = property.getAnnotation(RevisionTimestamp.class);
        final ModifiedEntityNames modifiedEntityNames = property.getAnnotation(ModifiedEntityNames.class);
        if (revisionNumber != null) {
            if (revisionNumberFound.isSet()) {
                throw new MappingException("Only one property may be annotated with @RevisionNumber!");
            }
            final XClass revisionNumberClass = property.getType();
            if (reflectionManager.equals(revisionNumberClass, Integer.class) || reflectionManager.equals(revisionNumberClass, Integer.TYPE)) {
                revisionInfoIdData = new PropertyData(property.getName(), property.getName(), accessType, null);
                revisionNumberFound.set();
            } else if (reflectionManager.equals(revisionNumberClass, Long.class) || reflectionManager.equals(revisionNumberClass, Long.TYPE)) {
                revisionInfoIdData = new PropertyData(property.getName(), property.getName(), accessType, null);
                revisionNumberFound.set();
                // The default is integer
                revisionPropType = "long";
            } else {
                throw new MappingException("The field annotated with @RevisionNumber must be of type " + "int, Integer, long or Long");
            }
            // Getting the @Column definition of the revision number property, to later use that info to
            // generate the same mapping for the relation from an audit table's revision number to the
            // revision entity revision number.
            final Column revisionPropColumn = property.getAnnotation(Column.class);
            if (revisionPropColumn != null) {
                revisionPropSqlType = revisionPropColumn.columnDefinition();
            }
        }
        if (revisionTimestamp != null) {
            if (revisionTimestampFound.isSet()) {
                throw new MappingException("Only one property may be annotated with @RevisionTimestamp!");
            }
            final XClass revisionTimestampClass = property.getType();
            if (reflectionManager.equals(revisionTimestampClass, Long.class) || reflectionManager.equals(revisionTimestampClass, Long.TYPE) || reflectionManager.equals(revisionTimestampClass, Date.class) || reflectionManager.equals(revisionTimestampClass, java.sql.Date.class)) {
                revisionInfoTimestampData = new PropertyData(property.getName(), property.getName(), accessType, null);
                revisionTimestampFound.set();
            } else {
                throw new MappingException("The field annotated with @RevisionTimestamp must be of type " + "long, Long, java.util.Date or java.sql.Date");
            }
        }
        if (modifiedEntityNames != null) {
            if (modifiedEntityNamesFound.isSet()) {
                throw new MappingException("Only one property may be annotated with @ModifiedEntityNames!");
            }
            final XClass modifiedEntityNamesClass = property.getType();
            if (reflectionManager.equals(modifiedEntityNamesClass, Set.class) && reflectionManager.equals(property.getElementClass(), String.class)) {
                modifiedEntityNamesData = new PropertyData(property.getName(), property.getName(), accessType, null);
                modifiedEntityNamesFound.set();
            } else {
                throw new MappingException("The field annotated with @ModifiedEntityNames must be of Set<String> type.");
            }
        }
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) ModifiedEntityNames(org.hibernate.envers.ModifiedEntityNames) PropertyData(org.hibernate.envers.internal.entities.PropertyData) Set(java.util.Set) RevisionTimestamp(org.hibernate.envers.RevisionTimestamp) RevisionNumber(org.hibernate.envers.RevisionNumber) XClass(org.hibernate.annotations.common.reflection.XClass) Date(java.util.Date) MappingException(org.hibernate.MappingException) Column(javax.persistence.Column)

Example 2 with Column

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

the class ORMConfigBeanUtil method getColumnsOrFields.

/**
	 * get columns through fields
	 * 
	 * @param clazz
	 * @param fields
	 * @param type
	 *            1.getColumns 2.getFields 3.allColumns 4.allFields
	 * 
	 * @return
	 */
private static <T> String[] getColumnsOrFields(T t, String[] strs, int type) {
    Class<?> clazz;
    if (t instanceof Class) {
        clazz = (Class<?>) t;
    } else {
        clazz = t.getClass();
    }
    if (!(t instanceof Class) && Map.class.isAssignableFrom(clazz)) {
        HashMap<String, Object> map = (HashMap<String, Object>) t;
        return (String[]) map.get("columns");
    }
    if (strs == null)
        strs = new String[] { "" };
    String[] result = strs.clone();
    List<String> list = new ArrayList<String>();
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null) {
        try {
            ReflectUtil ru = new ReflectUtil(clazz);
            // String idColumn = getIdColumn(clazz);
            for (int i = 0; i < strs.length; i++) {
                boolean finished = false;
                Field[] _fields = ru.getFields();
                for (Field _f : _fields) {
                    String pName = _f.getName();
                    JoinColumn jc = _f.getAnnotation(JoinColumn.class);
                    Column c = _f.getAnnotation(Column.class);
                    String col = pName;
                    if (c != null && c.name().trim().length() > 0)
                        col = c.name();
                    if (jc != null && jc.name().trim().length() > 0)
                        col = jc.name();
                    if (finished)
                        break;
                    switch(type) {
                        case 1:
                            int dotIndex = strs[i].indexOf(".");
                            if (dotIndex > 0 && dotIndex < strs[i].length() - 1) {
                                String[] dots = strs[i].split("\\.");
                                StringBuilder builder = new StringBuilder();
                                Class<?> prevCls = null;
                                for (int j = 0; j < dots.length; j++) {
                                    String dot = dots[j];
                                    Field field = ru.getField(dot);
                                    if (field == null && dot.equals(clazz.getSimpleName().toLowerCase())) {
                                        if (builder.length() > 0)
                                            builder.append(".");
                                        builder.append(dot);
                                        prevCls = clazz;
                                    } else if (field != null) {
                                        if (j == dots.length - 1) {
                                            if (builder.length() > 0)
                                                builder.append(".");
                                            builder.append(ORMConfigBeanUtil.getColumn(prevCls, dot));
                                        } else {
                                            Class<?> cls = ClassUtil.getGenericType(field);
                                            if (cls != null) {
                                                if (ORMConfigBeanCache.get(cls.getName()) != null) {
                                                    if (builder.length() > 0)
                                                        builder.append(".");
                                                    builder.append(cls.getSimpleName().toLowerCase());
                                                    prevCls = cls;
                                                    ru = new ReflectUtil(cls);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (builder.length() > 0) {
                                    result[i] = builder.toString();
                                    finished = true;
                                    break;
                                }
                            }
                            if (pName.equals(strs[i])) {
                                result[i] = col;
                                finished = true;
                            }
                            break;
                        case 2:
                            if (col.equals(strs[i])) {
                                result[i] = pName;
                                finished = true;
                            }
                            break;
                        case 3:
                            list.add(col);
                            break;
                        case 4:
                            list.add(pName);
                    }
                }
            }
        } catch (Throwable e) {
        }
    } else {
        try {
            ReflectUtil ru = new ReflectUtil(clazz);
            // String idColumn = getIdColumn(clazz);
            for (int i = 0; i < strs.length; i++) {
                boolean finished = false;
                List<Property> properties = ormBean.getProperty();
                for (Property p : properties) {
                    if (finished)
                        break;
                    switch(type) {
                        case 1:
                            int dotIndex = strs[i].indexOf(".");
                            if (dotIndex > 0 && dotIndex < strs[i].length() - 1) {
                                String[] dots = strs[i].split("\\.");
                                StringBuilder builder = new StringBuilder();
                                Class<?> prevCls = null;
                                for (int j = 0; j < dots.length; j++) {
                                    String dot = dots[j];
                                    Field field = ru.getField(dot);
                                    if (field == null && dot.equals(clazz.getSimpleName().toLowerCase())) {
                                        if (builder.length() > 0)
                                            builder.append(".");
                                        builder.append(dot);
                                        prevCls = clazz;
                                    } else if (field != null) {
                                        if (j == dots.length - 1) {
                                            if (builder.length() > 0)
                                                builder.append(".");
                                            builder.append(ORMConfigBeanUtil.getColumn(prevCls, dot));
                                        } else {
                                            Class<?> cls = ClassUtil.getGenericType(field);
                                            if (cls != null) {
                                                if (ORMConfigBeanCache.get(cls.getName()) != null) {
                                                    if (builder.length() > 0)
                                                        builder.append(".");
                                                    builder.append(cls.getSimpleName().toLowerCase());
                                                    prevCls = cls;
                                                    ru = new ReflectUtil(cls);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (builder.length() > 0) {
                                    result[i] = builder.toString();
                                    finished = true;
                                    break;
                                }
                            }
                            if (p.getName().equals(strs[i])) {
                                result[i] = p.getColumn();
                                finished = true;
                            }
                            break;
                        case 2:
                            if (p.getColumn().equals(strs[i])) {
                                result[i] = p.getName();
                                finished = true;
                            }
                            break;
                        case 3:
                            list.add(p.getColumn());
                            break;
                        case 4:
                            list.add(p.getName());
                    }
                }
            }
        } catch (Exception e) {
        }
    }
    return list.isEmpty() ? result : list.toArray(new String[] {});
}
Also used : HashMap(java.util.HashMap) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.eweb4j.orm.config.bean.Property)

Example 3 with Column

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

the class ORMConfigBeanWriter method write.

/**
	 * 将某个Class写入到目标xml文件中
	 * 
	 * @param filePath
	 *            文件相对路径
	 * @param clazzs
	 * @return
	 */
public static String write(String filePath, Class<?>... clazzs) {
    String error = null;
    File file = new File(ConfigConstant.CONFIG_BASE_PATH + filePath);
    if (filePath != null && clazzs != null && clazzs.length > 0) {
        List<ORMConfigBean> ormList = new ArrayList<ORMConfigBean>();
        try {
            for (int i = 0; i < clazzs.length; ++i) {
                Class<?> clazz = clazzs[i];
                String clsName = clazz.getSimpleName();
                Entity entity = clazz.getAnnotation(Entity.class);
                if (entity == null && !clsName.endsWith("PO") && !clsName.endsWith("POJO") && !clsName.endsWith("Entity") && !clsName.endsWith("Model")) {
                    return null;
                }
                Table tableAnn = clazz.getAnnotation(Table.class);
                String table = tableAnn == null ? "" : tableAnn.name();
                table = "".equals(table.trim()) ? clsName : table;
                ORMConfigBean ormBean = new ORMConfigBean();
                ormBean.setClazz(clazz.getName());
                ormBean.setId(clazz.getSimpleName());
                ormBean.setTable(table);
                ReflectUtil ru;
                ru = new ReflectUtil(clazz);
                List<Property> pList = new ArrayList<Property>();
                for (Field f : ru.getFields()) {
                    String name = f.getName();
                    Method getter = ru.getGetter(name);
                    Column colAnn = getter.getAnnotation(Column.class);
                    if (colAnn == null) {
                        colAnn = f.getAnnotation(Column.class);
                        if (colAnn == null)
                            continue;
                    }
                    Id idAnn = f.getAnnotation(Id.class);
                    if (idAnn == null) {
                        idAnn = getter.getAnnotation(Id.class);
                        if (idAnn == null)
                            continue;
                    }
                    Property p = new Property();
                    p.setAutoIncrement("1");
                    p.setPk("1");
                    Pk pkAnn = f.getAnnotation(Pk.class);
                    if (pkAnn != null) {
                        p.setPk("1");
                    }
                    String column = colAnn.name();
                    column = "".equals(column.trim()) ? name : column;
                    p.setName(name);
                    p.setColumn(column);
                    p.setType(f.getType().getName());
                    pList.add(p);
                }
                ormBean.setProperty(pList);
                ormList.add(ormBean);
            }
            BeanXMLUtil.getBeanXMLWriter(file, ormList).write();
        } catch (Exception e) {
            error = e.getMessage();
            e.printStackTrace();
        }
    } else {
        error = "Class参数不能为空";
    }
    return error;
}
Also used : Entity(javax.persistence.Entity) Table(javax.persistence.Table) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) Column(javax.persistence.Column) Id(javax.persistence.Id) Pk(org.eweb4j.orm.annotation.Pk) File(java.io.File) Property(org.eweb4j.orm.config.bean.Property)

Example 4 with Column

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

the class Tools method getAnnotatedSetters.

/**
     * Get all setter methods annotated with a given column name
     */
static final List<Method> getAnnotatedSetters(final Configuration configuration, final Class<?> type, final String name) {
    return Cache.run(configuration, new CachedOperation<List<Method>>() {

        @Override
        public List<Method> call() {
            List<Method> result = new ArrayList<Method>();
            for (Method method : getInstanceMethods(type)) {
                Column column = method.getAnnotation(Column.class);
                if (column != null && namesMatch(name, column.name())) {
                    // Annotated setter
                    if (method.getParameterTypes().length == 1) {
                        result.add(accessible(method));
                    } else // Annotated getter with matching setter
                    if (method.getParameterTypes().length == 0) {
                        String m = method.getName();
                        String suffix = m.startsWith("get") ? m.substring(3) : m.startsWith("is") ? m.substring(2) : null;
                        if (suffix != null) {
                            try {
                                Method setter = type.getMethod("set" + suffix, method.getReturnType());
                                // Setter annotation is more relevant
                                if (setter.getAnnotation(Column.class) == null) {
                                    result.add(accessible(setter));
                                }
                            } catch (NoSuchMethodException ignore) {
                            }
                        }
                    }
                }
            }
            return result;
        }
    }, DATA_REFLECTION_CACHE_GET_ANNOTATED_SETTERS, type, name);
}
Also used : Column(javax.persistence.Column) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method)

Example 5 with Column

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

the class Filter method addOrderBy.

public void addOrderBy(Class<?> clazz, String field, boolean ascending) {
    if (field == null) {
        return;
    }
    Field f;
    Pair<Class<?>, Field> pair = ReflectUtil.getAnyField(clazz, field);
    assert (pair != null) : "Can't find field " + field + " in " + clazz.getName();
    clazz = pair.first();
    f = pair.second();
    Column column = f.getAnnotation(Column.class);
    String name = column != null ? column.name() : field;
    StringBuilder order = new StringBuilder();
    if (column.table() == null || column.table().length() == 0) {
        order.append(DbUtil.getTableName(clazz));
    } else {
        order.append(column.table());
    }
    order.append(".").append(name).append(ascending ? " ASC " : " DESC ");
    if (_orderBy == null) {
        _orderBy = order.insert(0, " ORDER BY ").toString();
    } else {
        _orderBy = order.insert(0, _orderBy).toString();
    }
}
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