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;
}
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();
}
}
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();
}
}
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);
}
}
}
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);
}
}
}
Aggregations