Search in sources :

Example 26 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class SelectDAOImpl method selectBySQL.

public <T> List<T> selectBySQL(Class<T> clazz, String sql, Object... args) throws DAOException {
    List<T> result = null;
    Connection con = null;
    try {
        con = ds.getConnection();
        result = JdbcUtil.getListWithArgs(con, clazz, sql, args);
    } catch (Exception e) {
        throw new DAOException("", e);
    }
    return result;
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Connection(java.sql.Connection) DAOException(org.eweb4j.orm.dao.DAOException)

Example 27 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class TestCascadeDAO method testManyDelete.

public static void testManyDelete() {
    List<Master> masterList;
    try {
        masterList = DAOFactory.getSelectDAO().selectAll(Master.class);
        if (masterList != null) {
            for (Master m : masterList) {
                System.out.println(m + "|" + m.getPets());
                DAOFactory.getCascadeDAO().delete(m, "pets");
            }
        }
    } catch (DAOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Master(test.po.Master) DAOException(org.eweb4j.orm.dao.DAOException)

Example 28 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class TestCascadeDAO method testOneSelect.

public static void testOneSelect() {
    List<Pet> petList;
    try {
        petList = DAOFactory.getSelectDAO().selectAll(Pet.class);
        if (petList != null) {
            for (Pet p : petList) {
                System.out.println(p + "|" + p.getMaster());
                DAOFactory.getCascadeDAO().select(p, "master");
                System.out.println(p.getMaster());
            // break;
            }
        }
    } catch (DAOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Pet(test.po.Pet)

Example 29 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class ManyToManyDAO method init.

public void init(Object t, List<Field> fields) throws DAOException {
    this.t = t;
    this.fields = fields;
    this.ru = new ReflectUtil(this.t);
    this.table = ORMConfigBeanUtil.getTable(this.t.getClass(), true);
    String idField = ORMConfigBeanUtil.getIdField(this.t.getClass());
    //		Method idSetter = ru.getSetter(idField);
    //		if (idSetter == null)
    //			throw new DAOException("can not get idSetter.", null);
    Method idGetter = ru.getGetter(idField);
    if (idGetter != null) {
        try {
            Object _idVal = idGetter.invoke(this.t);
            this.idVal = _idVal == null ? null : String.valueOf(_idVal);
        } catch (Exception e) {
            throw new DAOException(idGetter + " invoke exception ", e);
        }
    }
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) ReflectUtil(org.eweb4j.util.ReflectUtil) Method(java.lang.reflect.Method) DAOException(org.eweb4j.orm.dao.DAOException)

Example 30 with DAOException

use of org.eweb4j.orm.dao.DAOException in project eweb4j-framework by laiweiwei.

the class ManyToManyDAO method update.

/**
	 * 一对多级联更新
	 */
public void update(Long newFromRefVal) {
    if (this.fields == null || this.fields.size() == 0)
        return;
    // "update {table} set {fromRefCol} = {newFromRefVal} where {fromRefCol} = {fromRefVal}
    // ; update {relTable} set {from} = {newFromRefVal} where {from} = {fromRefVal}"
    String format = "update %s set %s = %s where %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;
        // 第三方关系表
        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 {
            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);
            if (_obj == null)
                continue;
            String fromRefVal = String.valueOf(_obj);
            // "update {table} set {fromRefCol} = {newFromRefVal} where {fromRefCol} = {fromRefVal}
            // ; update {relTable} set {from} = {newFromRefVal} where {from} = {fromRefVal}"
            final String sql1 = String.format(format, table, fromRefCol, newFromRefVal, fromRefCol, fromRefVal);
            final String sql2 = String.format(format, relTable, from, newFromRefVal, from, fromRefVal);
            Transaction.execute(new Trans() {

                @Override
                public void run(Object... args) throws Exception {
                    DAOFactory.getUpdateDAO(dsName).updateBySQL(sql1);
                    DAOFactory.getUpdateDAO(dsName).updateBySQL(sql2);
                }
            });
        } catch (Exception e) {
            throw new DAOException("", e);
        }
    }
}
Also used : DAOException(org.eweb4j.orm.dao.DAOException) Field(java.lang.reflect.Field) JoinColumn(javax.persistence.JoinColumn) ManyToMany(javax.persistence.ManyToMany) Method(java.lang.reflect.Method) Trans(org.eweb4j.orm.jdbc.transaction.Trans) DAOException(org.eweb4j.orm.dao.DAOException) JoinTable(javax.persistence.JoinTable)

Aggregations

DAOException (org.eweb4j.orm.dao.DAOException)56 Connection (java.sql.Connection)42 Sql (org.eweb4j.orm.sql.Sql)13 Method (java.lang.reflect.Method)9 ReflectUtil (org.eweb4j.util.ReflectUtil)9 Field (java.lang.reflect.Field)7 ArrayList (java.util.ArrayList)6 JoinColumn (javax.persistence.JoinColumn)6 JoinTable (javax.persistence.JoinTable)6 ManyToMany (javax.persistence.ManyToMany)3 ManyToOne (javax.persistence.ManyToOne)3 OneToMany (javax.persistence.OneToMany)3 Trans (org.eweb4j.orm.jdbc.transaction.Trans)3 Master (test.po.Master)3 List (java.util.List)2 OrderBy (javax.persistence.OrderBy)2 Pet (test.po.Pet)2 OneToOne (javax.persistence.OneToOne)1