Search in sources :

Example 1 with OrderBy

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

the class OneToManyDAO method select.

/**
	 * 一对多(主从)级联查询
	 */
public void select() throws DAOException {
    if (this.fields == null || this.fields.size() == 0)
        return;
    Class<?> ownClass = ru.getObject().getClass();
    String fromRefVal = null;
    for (Field f : fields) {
        Method tarGetter = ru.getGetter(f.getName());
        if (tarGetter == null)
            continue;
        OneToMany ann = tarGetter.getAnnotation(OneToMany.class);
        if (ann == null) {
            ann = f.getAnnotation(OneToMany.class);
            if (ann == null)
                continue;
        }
        OrderBy orderAnn = tarGetter.getAnnotation(OrderBy.class);
        if (orderAnn == null)
            orderAnn = f.getAnnotation(OrderBy.class);
        Class<?> tarClass = ann.targetEntity();
        if (void.class.isAssignableFrom(tarClass))
            tarClass = ClassUtil.getGenericType(f);
        String orderBy = "";
        if (orderAnn != null && orderAnn.value().trim().length() > 0)
            orderBy = " ORDER BY " + orderAnn.value().replace("t.", tarClass.getSimpleName().toLowerCase() + ".");
        String mappedBy = ann.mappedBy();
        try {
            ReflectUtil tarRu = new ReflectUtil(tarClass);
            List<?> tarList = null;
            JoinTable joinTable = null;
            if (f.isAnnotationPresent(JoinTable.class)) {
                joinTable = f.getAnnotation(JoinTable.class);
            } else if (tarGetter.isAnnotationPresent(JoinTable.class)) {
                joinTable = tarGetter.getAnnotation(JoinTable.class);
            }
            // 如果用户填写了 JoinTable注解,说明是第三方表建立的关联关系,需要查询第三方表和字段才能获取到targetList
            if (joinTable != null) {
                JoinColumn[] froms = joinTable.joinColumns();
                if (froms == null || froms.length == 0)
                    continue;
                String tarTable = joinTable.name();
                String from = froms[0].name();
                String fromRefCol = froms[0].referencedColumnName();
                if (fromRefCol == null || fromRefCol.trim().length() == 0)
                    fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
                String fromRefField = ORMConfigBeanUtil.getField(t.getClass(), fromRefCol);
                Method fromRefFieldGetter = ru.getGetter(fromRefField);
                if (fromRefFieldGetter == null)
                    throw new Exception("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method");
                Object _obj = fromRefFieldGetter.invoke(t);
                if (_obj == null)
                    continue;
                fromRefVal = String.valueOf(_obj);
                String format = "select %s from %s where %s = ?  ;";
                String sql = String.format(format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), tarTable, from) + orderBy;
                // finished
                tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, fromRefVal);
            } else {
                // 否则的话按照ManyToOne去查询
                // 如果给定了 mappedBy,直接用这个mappedBy来获取filed,否则遍历。
                Field mappedField = null;
                if (mappedBy != null && mappedBy.trim().length() > 0) {
                    mappedField = tarRu.getField(mappedBy);
                } else {
                    for (Field field : tarRu.getFields()) {
                        if (!field.getType().getName().equals(ownClass.getName()))
                            continue;
                        mappedField = field;
                        mappedBy = mappedField.getName();
                        break;
                    }
                }
                if (mappedField == null)
                    throw new Exception("mapped field of " + tarClass + " not found");
                Method tarObjFieldGetter = tarRu.getGetter(mappedBy);
                if (tarObjFieldGetter == null)
                    continue;
                ManyToOne manyToOne = mappedField.getAnnotation(ManyToOne.class);
                if (manyToOne == null)
                    manyToOne = tarObjFieldGetter.getAnnotation(ManyToOne.class);
                if (manyToOne == null)
                    continue;
                String fromRefCol = null;
                JoinColumn joinCol = mappedField.getAnnotation(JoinColumn.class);
                if (joinCol == null)
                    joinCol = tarObjFieldGetter.getAnnotation(JoinColumn.class);
                if (joinCol != null)
                    fromRefCol = joinCol.referencedColumnName();
                if (fromRefCol == null || fromRefCol.trim().length() == 0)
                    fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
                String fromRefField = ORMConfigBeanUtil.getField(ownClass, fromRefCol);
                Method fromRefFieldGetter = ru.getGetter(fromRefField);
                if (fromRefFieldGetter == null)
                    throw new Exception("can not find the 'from ref field field -> " + fromRefField + "' of " + ownClass + " 's getter method");
                Object _obj = fromRefFieldGetter.invoke(t);
                if (_obj != null)
                    fromRefVal = String.valueOf(_obj);
                String format = "select %s from %s where %s = ?  ;";
                String sql = String.format(format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), ORMConfigBeanUtil.getTable(tarClass, true), ORMConfigBeanUtil.getColumn(tarClass, mappedBy)) + orderBy;
                // finished
                tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, fromRefVal);
            }
            if (tarList == null)
                continue;
            Method tarSetter = ru.getSetter(f.getName());
            if (tarSetter == null)
                continue;
            tarSetter.invoke(t, tarList);
        } catch (Exception e) {
            throw new DAOException("", e);
        }
    }
}
Also used : OrderBy(javax.persistence.OrderBy) Method(java.lang.reflect.Method) OneToMany(javax.persistence.OneToMany) DAOException(org.eweb4j.orm.dao.DAOException) ManyToOne(javax.persistence.ManyToOne) DAOException(org.eweb4j.orm.dao.DAOException) Field(java.lang.reflect.Field) ReflectUtil(org.eweb4j.util.ReflectUtil) JoinColumn(javax.persistence.JoinColumn) JoinTable(javax.persistence.JoinTable)

Example 2 with OrderBy

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

the class ManyToManyDAO method select.

/**
	 * 多对多级联查询 
	 * 1.当主对象没有包含任何一个关联对象时,默认查询所有与之关联的对象
	 * 2.当主对象中包含了关联对象时(含有其toRefVal值),则只查询这些关联的对象
	 * 
	 */
public void select() throws DAOException {
    if (this.fields == null || this.fields.size() == 0)
        return;
    // select tarClass from {tarTable} t, {relTable} r where r.to = t.toRefCol and r.from = {fromRefVal} order by r.xxx desc
    // select %s from {tarTable} where {toRefCol} in (select {to} from {relTable} where {from} = {fromRefVal} order by xxx desc)
    //		String format = "SELECT %s FROM %s WHERE %s IN (SELECT %s FROM %s WHERE %s = ? %s)";
    final String format = "select %s from %s, %s r where r.%s = %s.%s and r.%s = ? %s ";
    for (Field f : fields) {
        Method tarGetter = ru.getGetter(f.getName());
        if (tarGetter == null)
            continue;
        ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class);
        if (ann == null) {
            ann = f.getAnnotation(ManyToMany.class);
            if (ann == null)
                continue;
        }
        JoinTable join = tarGetter.getAnnotation(JoinTable.class);
        if (join == null) {
            join = f.getAnnotation(JoinTable.class);
            if (join == null)
                continue;
        }
        JoinColumn[] froms = join.joinColumns();
        if (froms == null || froms.length == 0)
            continue;
        JoinColumn[] tos = join.inverseJoinColumns();
        if (tos == null || tos.length == 0)
            continue;
        // 多对多关系目标Class
        Class<?> tarClass = ann.targetEntity();
        if (void.class.isAssignableFrom(tarClass)) {
            tarClass = ClassUtil.getGenericType(f);
        }
        String tarTable = ORMConfigBeanUtil.getTable(tarClass, true);
        OrderBy orderAnn = tarGetter.getAnnotation(OrderBy.class);
        if (orderAnn == null)
            orderAnn = f.getAnnotation(OrderBy.class);
        String orderBy = "";
        if (orderAnn != null && orderAnn.value().trim().length() > 0)
            orderBy = " ORDER BY " + orderAnn.value().replace("t.", tarClass.getSimpleName().toLowerCase() + ".");
        // 目标类对应的数据库表Id字段
        String toRefCol = tos[0].referencedColumnName();
        if (toRefCol == null || toRefCol.trim().length() == 0)
            toRefCol = ORMConfigBeanUtil.getIdColumn(tarClass);
        String toRefField = ORMConfigBeanUtil.getField(tarClass, toRefCol);
        // 目标类在第三方关系表中的字段名
        String to = tos[0].name();
        // 第三方关系表
        String relTable = join.name();
        // 主类在第三方关系表中的字段名
        String from = froms[0].name();
        String fromRefCol = froms[0].referencedColumnName();
        if (fromRefCol == null || fromRefCol.trim().length() == 0)
            fromRefCol = ORMConfigBeanUtil.getIdColumn(t);
        String fromRefField = ORMConfigBeanUtil.getField(t.getClass(), fromRefCol);
        try {
            List<?> tarList = null;
            tarList = (List<?>) tarGetter.invoke(t);
            if (tarList != null && tarList.size() > 0) {
                for (int i = 0; i < tarList.size(); i++) {
                    Object tarObj = tarList.get(i);
                    ReflectUtil tarRu = new ReflectUtil(tarObj);
                    Method toRefFieldGetter = tarRu.getGetter(toRefField);
                    if (toRefFieldGetter == null)
                        throw new DAOException("can not find the 'to ref field -> " + toRefField + "' of " + tarClass + " 's getter method", null);
                    Object _obj = toRefFieldGetter.invoke(tarObj);
                    if (_obj == null)
                        continue;
                    String toRefVal = String.valueOf(_obj);
                    // 查询 select %s from {tarTable} where {tarIdColumn} = {tarIdVal}
                    tarObj = DAOFactory.getSelectDAO(dsName).selectOne(tarClass, new String[] { toRefField }, new String[] { toRefVal });
                }
            } else {
                // "select %s from %s, %s r where r.%s = %s.%s and r.%s = ? %s "
                // select tarClass from {tarTable} t, {relTable} r where r.to = t.toRefCol and r.from = ? order by r.xxx desc
                String sql = String.format(format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), tarTable, relTable, to, tarClass.getSimpleName().toLowerCase(), toRefCol, from, orderBy);
                // 从数据库中取出与当前主对象fromRefCol关联的所有目标对象,
                Method fromRefFieldGetter = ru.getGetter(fromRefField);
                if (fromRefFieldGetter == null)
                    throw new DAOException("can not find the 'from ref field -> " + fromRefField + "' of " + t.getClass() + " 's getter method", null);
                Object _obj = fromRefFieldGetter.invoke(t);
                String fromRefVal = null;
                if (_obj != null)
                    fromRefVal = String.valueOf(_obj);
                tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, fromRefVal);
            }
            // 并注入到当前主对象的属性中
            Method tarSetter = ru.getSetter(f.getName());
            tarSetter.invoke(t, tarList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException("", e);
        }
    }
}
Also used : OrderBy(javax.persistence.OrderBy) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) DAOException(org.eweb4j.orm.dao.DAOException) DAOException(org.eweb4j.orm.dao.DAOException) Field(java.lang.reflect.Field) ReflectUtil(org.eweb4j.util.ReflectUtil) JoinColumn(javax.persistence.JoinColumn) JoinTable(javax.persistence.JoinTable)

Aggregations

Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 JoinColumn (javax.persistence.JoinColumn)2 JoinTable (javax.persistence.JoinTable)2 OrderBy (javax.persistence.OrderBy)2 DAOException (org.eweb4j.orm.dao.DAOException)2 ReflectUtil (org.eweb4j.util.ReflectUtil)2 ManyToMany (javax.persistence.ManyToMany)1 ManyToOne (javax.persistence.ManyToOne)1 OneToMany (javax.persistence.OneToMany)1