Search in sources :

Example 1 with OneToOne

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

the class DAOImpl method queryBySql.

public <T> Collection<T> queryBySql(final String sql) {
    Class<T> mappingCls = null;
    if (this.targetEntity == null)
        mappingCls = (Class<T>) this.clazz;
    else
        mappingCls = (Class<T>) this.targetEntity;
    List<T> result = null;
    try {
        if (Map.class.isAssignableFrom(mappingCls)) {
            Connection con = ds.getConnection();
            if (args != null && args.size() > 0) {
                result = (List<T>) JdbcUtil.getListWithArgs(con, mappingCls, sql, args.toArray(new Object[] {}));
            } else {
                result = (List<T>) JdbcUtil.getList(con, mappingCls, sql);
            }
        } else {
            if (args != null && args.size() > 0) {
                result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql, args.toArray(new Object[] {}));
            } else {
                result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql);
            }
        }
        //this.clear();
        if (result != null && result.size() > 0) {
            for (T t : result) {
                // ToOne relation class cascade select
                ReflectUtil ru = new ReflectUtil(t);
                for (Field field : ru.getFields()) {
                    String f = field.getName();
                    OneToOne o2o = field.getAnnotation(OneToOne.class);
                    ManyToOne m2o = field.getAnnotation(ManyToOne.class);
                    OneToMany o2m = field.getAnnotation(OneToMany.class);
                    ManyToMany m2m = field.getAnnotation(ManyToMany.class);
                    FetchType fetchType = null;
                    boolean is2One = false;
                    if (o2o != null) {
                        fetchType = o2o.fetch();
                        is2One = true;
                    }
                    if (m2o != null) {
                        fetchType = m2o.fetch();
                        is2One = true;
                    }
                    if (o2m != null) {
                        fetchType = o2m.fetch();
                    }
                    if (m2m != null) {
                        fetchType = m2m.fetch();
                    }
                    if (fetchType == null)
                        continue;
                    String beanId = field.getType().getName();
                    if (!is2One)
                        beanId = ClassUtil.getGenericType(field).getName();
                    boolean isEntity = ORMConfigBeanCache.containsKey(beanId);
                    if (!isEntity)
                        continue;
                    if (unFetch.contains(f))
                        continue;
                    if (fetch.contains(f)) {
                        log.debug("cascade select -> " + t.getClass().getName() + "." + f);
                        DAOFactory.getCascadeDAO(dsName).select(t, f);
                        continue;
                    }
                    if (FetchType.LAZY.equals(fetchType))
                        continue;
                    log.debug("cascade select -> " + t.getClass().getName() + "." + f);
                    DAOFactory.getCascadeDAO(dsName).select(t, f);
                }
            }
        }
        return result;
    } catch (Exception e) {
        log.error("sql-->" + sql, e);
        throw new DAOException(sql + " execute exception", e);
    }
}
Also used : FetchType(javax.persistence.FetchType) Connection(java.sql.Connection) ManyToMany(javax.persistence.ManyToMany) OneToMany(javax.persistence.OneToMany) ManyToOne(javax.persistence.ManyToOne) SQLException(java.sql.SQLException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne)

Example 2 with OneToOne

use of javax.persistence.OneToOne in project orientdb by orientechnologies.

the class OObjectEntitySerializer method getSpecifiedLinkedType.

public static Class<?> getSpecifiedLinkedType(final Field f) {
    final ManyToOne m = f.getAnnotation(ManyToOne.class);
    if (m != null && !m.targetEntity().equals(void.class))
        return m.targetEntity();
    final OneToOne m2 = f.getAnnotation(OneToOne.class);
    if (m2 != null && !m2.targetEntity().equals(void.class))
        return m2.targetEntity();
    return null;
}
Also used : OneToOne(javax.persistence.OneToOne) ManyToOne(javax.persistence.ManyToOne)

Example 3 with OneToOne

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

the class InsertSqlCreator method create.

public Sql[] create(String condition) throws SqlCreateException {
    Sql[] sqls = new Sql[ts.length];
    for (int index = 0; index < ts.length; ++index) {
        String table = null;
        StringBuilder columnSB = new StringBuilder();
        StringBuilder valueSB = new StringBuilder();
        T t = ts[index];
        Class<?> clazz = t.getClass();
        String[] fields;
        String[] columns;
        String idColumn;
        Object[] values = null;
        sqls[index] = new Sql();
        HashMap<String, Object> map = null;
        if (Map.class.isAssignableFrom(clazz)) {
            map = (HashMap<String, Object>) t;
            table = String.valueOf(map.get("table"));
            idColumn = String.valueOf(map.get("idColumn"));
            if (idColumn == null)
                idColumn = "id";
            fields = (String[]) map.get("columns");
            columns = fields;
            values = (Object[]) map.get("values");
        } else {
            table = ORMConfigBeanUtil.getTable(clazz, false);
            idColumn = ORMConfigBeanUtil.getIdColumn(t.getClass());
            fields = ORMConfigBeanUtil.getFields(clazz);
            columns = ORMConfigBeanUtil.getColumns(clazz);
        }
        ReflectUtil ru = new ReflectUtil(t);
        for (int i = 0; i < columns.length; i++) {
            String column = columns[i];
            Object value = null;
            if (map != null && values != null) {
                value = values[i];
            } else {
                String name = fields[i];
                Method getter = ru.getGetter(name);
                if (getter == null)
                    continue;
                Object _value = null;
                try {
                    _value = getter.invoke(t);
                    if (_value == null)
                        continue;
                    if (ClassUtil.isPojo(_value.getClass())) {
                        Field f = ru.getField(name);
                        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) {
                            JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
                            if (joinColAnn == null)
                                joinColAnn = f.getAnnotation(JoinColumn.class);
                            if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                                String refCol = joinColAnn.referencedColumnName();
                                String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                                ReflectUtil tarRu = new ReflectUtil(_value);
                                Method tarFKGetter = tarRu.getGetter(refField);
                                value = tarFKGetter.invoke(_value);
                            } else {
                                ReflectUtil tarRu = new ReflectUtil(_value);
                                String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                                if (tarFKField != null) {
                                    Method tarFKGetter = tarRu.getGetter(tarFKField);
                                    value = tarFKGetter.invoke(_value);
                                }
                            }
                        }
                        if (value == null)
                            continue;
                    } else {
                        value = _value;
                    }
                } catch (Exception e) {
                    throw new SqlCreateException(getter + " invoke exception " + e.toString(), e);
                }
            }
            // id 字段不允许插入表中
            if (idColumn != null && idColumn.equalsIgnoreCase(column))
                continue;
            if (columnSB.length() > 0)
                columnSB.append(",");
            columnSB.append(column);
            if (valueSB.length() > 0)
                valueSB.append(",");
            //				valueSB.append("'").append(value).append("'");
            valueSB.append("?");
            sqls[index].args.add(value);
        }
        String format = "INSERT INTO ${table}(${columns}) VALUES(${values}) ${condition} ;";
        format = format.replace("${table}", table);
        format = format.replace("${columns}", columnSB.toString());
        format = format.replace("${values}", valueSB.toString());
        if (condition != null)
            format = format.replace("${condition}", " WHERE " + condition);
        else
            format = format.replace("${condition} ", "");
        sqls[index].sql = format;
    }
    return sqls;
}
Also used : Method(java.lang.reflect.Method) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn)

Example 4 with OneToOne

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

the class InsertSqlCreator method createByFieldsIsValues.

public Sql[] createByFieldsIsValues(String[][] fields, Object[][] values) throws SqlCreateException {
    Sql[] sqls = new Sql[ts.length];
    for (int i = 0; i < ts.length; ++i) {
        T t = ts[i];
        ReflectUtil ru = new ReflectUtil(t);
        Class<?> clazz = t.getClass();
        String table = ORMConfigBeanUtil.getTable(clazz, false);
        StringBuilder columnSB = new StringBuilder();
        StringBuilder valueSB = new StringBuilder();
        sqls[i] = new Sql();
        for (int j = 0; j < fields[i].length; ++j) {
            String name = fields[i][j];
            Object _value = null;
            Object value = null;
            if (values == null) {
                Method getter = null;
                try {
                    getter = ru.getGetter(name);
                    if (getter == null)
                        continue;
                    _value = getter.invoke(t);
                    if (_value == null)
                        continue;
                    if (ClassUtil.isPojo(_value.getClass())) {
                        Field f = ru.getField(fields[i][j]);
                        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) {
                            JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
                            if (joinColAnn == null)
                                joinColAnn = f.getAnnotation(JoinColumn.class);
                            if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                                String refCol = joinColAnn.referencedColumnName();
                                String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                                ReflectUtil tarRu = new ReflectUtil(_value);
                                Method tarFKGetter = tarRu.getGetter(refField);
                                value = tarFKGetter.invoke(_value);
                            } else {
                                ReflectUtil tarRu = new ReflectUtil(_value);
                                String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                                if (tarFKField != null) {
                                    Method tarFKGetter = tarRu.getGetter(tarFKField);
                                    value = tarFKGetter.invoke(_value);
                                }
                            }
                        }
                        if (value == null)
                            continue;
                    } else {
                        value = _value;
                    }
                } catch (Exception e) {
                    throw new SqlCreateException(getter + " invoke exception " + e.toString(), e);
                }
            } else {
                value = values[i][j];
            }
            String column = ORMConfigBeanUtil.getColumn(clazz, name);
            if (valueSB.length() > 0) {
                columnSB.append(",");
                valueSB.append(",");
            }
            columnSB.append(column);
            //				valueSB.append("'").append(value).append("'");
            valueSB.append("?");
            sqls[i].args.add(value);
        }
        sqls[i].sql = String.format("INSERT INTO %s(%s) VALUES(%s) ;", table, columnSB.toString(), valueSB.toString());
    }
    return sqls;
}
Also used : Method(java.lang.reflect.Method) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn)

Example 5 with OneToOne

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

the class SelectSqlCreator method selectWhere.

/**
	 * 各种条件查询
	 * 
	 * @param fields
	 *            构成条件的属性名数组(填写的应该是对象属性名,不是数据库字段名)
	 * @param values
	 *            构成条件的字段值
	 * @param likeType
	 *            匹配类型 -1左匹配 0全匹配 1右匹配
	 * @param isLike
	 *            是否模糊查询
	 * @param orderField
	 *            对哪个字段进行排序
	 * @param oType
	 *            升序还是降序
	 * @return 字符串
	 * @param currentPage
	 *            第几页
	 * @param numPerPage
	 *            每页显示多少条
	 * @return
	 * @throws SqlCreateException
	 */
public String selectWhere(String[] fields, String[] values, int likeType, boolean isLike, boolean isNot, boolean isOR, String orderField, int oType, int currentPage, int numPerPage) throws SqlCreateException {
    String oper = " = ";
    String connector = " AND ";
    String left = " '";
    String right = "' ";
    if (isNot)
        oper = "<>";
    if (isLike) {
        oper = " LIKE ";
        left = " '%";
        right = "%' ";
        if (isNot)
            oper = " NOT LIKE ";
        switch(likeType) {
            case LikeType.LEFT_LIKE:
                left = " '";
                break;
            case LikeType.RIGHT_LIKE:
                right = "' ";
                break;
            case LikeType.ALL_LIKE:
        }
    }
    if (isOR)
        connector = " OR ";
    String[] type = { oper, connector, left, right };
    if (orderField == null) {
        orderField = idColumn;
        orderField = OrderColumnUtil.getOrderColumn(orderField, dbType);
    } else
        orderField = ORMConfigBeanUtil.getColumn(clazz, orderField);
    StringBuilder condition = new StringBuilder();
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    for (int i = 0; i < columns.length; ++i) {
        if (values != null)
            if (condition.length() > 0)
                condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
            else
                condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
        else {
            ReflectUtil ru = new ReflectUtil(t);
            Method getter = ru.getGetter(fields[i]);
            if (getter == null)
                continue;
            try {
                Object _value = getter.invoke(t);
                if (_value == null)
                    continue;
                Object value = null;
                if (ClassUtil.isPojo(_value.getClass())) {
                    Field f = ru.getField(fields[i]);
                    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) {
                        JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
                        if (joinColAnn == null)
                            joinColAnn = f.getAnnotation(JoinColumn.class);
                        if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                            String refCol = joinColAnn.referencedColumnName();
                            String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                            ReflectUtil tarRu = new ReflectUtil(_value);
                            Method tarFKGetter = tarRu.getGetter(refField);
                            value = tarFKGetter.invoke(_value);
                        } else {
                            ReflectUtil tarRu = new ReflectUtil(_value);
                            String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                            if (tarFKField != null) {
                                Method tarFKGetter = tarRu.getGetter(tarFKField);
                                value = tarFKGetter.invoke(_value);
                            }
                        }
                    }
                    if (value == null)
                        continue;
                } else
                    value = _value;
                if (condition.length() > 0)
                    condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
                else
                    condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
            } catch (Exception e) {
                e.printStackTrace();
                throw new SqlCreateException(getter + " invoke exception " + e.toString());
            }
        }
    }
    String orderType = OrderType.ASC_ORDER == oType ? "ASC" : "DESC";
    if (currentPage > 0 && numPerPage > 0)
        return divPage(currentPage, numPerPage, orderField, oType, condition.toString());
    else {
        return String.format("SELECT %s FROM %s WHERE %s ORDER BY %s %s ;", selectAllColumn, table, condition.toString(), orderField, orderType);
    }
}
Also used : Method(java.lang.reflect.Method) ManyToOne(javax.persistence.ManyToOne) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne) JoinColumn(javax.persistence.JoinColumn)

Aggregations

OneToOne (javax.persistence.OneToOne)21 ManyToOne (javax.persistence.ManyToOne)13 Field (java.lang.reflect.Field)9 JoinColumn (javax.persistence.JoinColumn)8 ReflectUtil (org.eweb4j.util.ReflectUtil)8 Method (java.lang.reflect.Method)7 ManyToMany (javax.persistence.ManyToMany)6 OneToMany (javax.persistence.OneToMany)6 Test (org.junit.Test)5 AnnotationException (org.hibernate.AnnotationException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Column (javax.persistence.Column)2 FetchType (javax.persistence.FetchType)2 Id (javax.persistence.Id)2 JoinTable (javax.persistence.JoinTable)2 AssertionFailure (org.hibernate.AssertionFailure)2 OAccess (com.orientechnologies.orient.core.annotation.OAccess)1 ODocumentInstance (com.orientechnologies.orient.core.annotation.ODocumentInstance)1 OId (com.orientechnologies.orient.core.annotation.OId)1