use of org.eweb4j.orm.jdbc.transaction.Trans in project eweb4j-framework by laiweiwei.
the class OneToManyDAO method delete.
/**
*
* 一对多(主从)级联删除
* 1.前提条件必须主对象要存在于数据库中
* 2.检查当前主对象中的关联对象,如果关联对象为空,则删除所有与主对象有关的关联关系。
* 3.如果当前主对象中含有关联对象,则删除这些关联对象与主对象的关系
* 4.不会删除主对象
*/
public void delete() throws DAOException {
if (this.fields == null || this.fields.size() == 0)
return;
final Class<?> ownClass = ru.getObject().getClass();
Transaction.execute(new Trans() {
@Override
public void run(Object... args) throws Exception {
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;
}
String mappedBy = ann.mappedBy();
Class<?> tarClass = ann.targetEntity();
if (void.class.isAssignableFrom(tarClass))
tarClass = ClassUtil.getGenericType(f);
List<?> tarList = null;
try {
tarList = (List<?>) tarGetter.invoke(t);
} catch (Exception e) {
throw new DAOException(tarGetter + " invoke exception ", e);
}
if (tarList == null || tarList.size() == 0) {
// 当关联对象为空的时候,删除所有关联对象
ReflectUtil tarRu = new ReflectUtil(tarClass);
if (mappedBy == null || mappedBy.trim().length() == 0) {
for (Field tarObjField : tarRu.getFields()) {
if (!tarObjField.getType().getName().equals(ownClass.getName()))
continue;
Method tarObjFieldGetter = tarRu.getGetter(tarObjField.getName());
if (tarObjFieldGetter == null)
continue;
ManyToOne manyToOne = tarObjField.getAnnotation(ManyToOne.class);
if (manyToOne == null)
manyToOne = tarObjFieldGetter.getAnnotation(ManyToOne.class);
if (manyToOne == null)
continue;
mappedBy = tarObjField.getName();
String fromRefCol = null;
JoinColumn joinCol = tarObjField.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");
String fromRefVal = null;
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj != null)
fromRefVal = String.valueOf(_obj);
DAOFactory.getDeleteDAO(dsName).deleteByFieldIsValue(tarClass, new String[] { tarObjField.getName() }, new String[] { fromRefVal });
break;
}
}
} else {
// 当关联对象不为空的时候,删除这些关联对象
for (int i = 0; i < tarList.size(); i++) {
Object tarObj = tarList.get(i);
if (tarObj == null)
continue;
//如果这些对象没有ID值,跳过
Object tarObjIdVal = ORMConfigBeanUtil.getIdVal(tarObj);
if (tarObjIdVal == null)
continue;
ReflectUtil tarRu = new ReflectUtil(tarObj);
if (mappedBy != null && mappedBy.trim().length() > 0) {
Method ownFieldSetter = tarRu.getSetter(mappedBy);
if (ownFieldSetter == null)
continue;
// finished
DAOFactory.getDeleteDAO(dsName).deleteById(tarObj);
} else {
JoinTable joinTable = null;
if (f.isAnnotationPresent(JoinTable.class)) {
joinTable = f.getAnnotation(JoinTable.class);
} else if (tarGetter.isAnnotationPresent(JoinTable.class)) {
joinTable = tarGetter.getAnnotation(JoinTable.class);
} else {
// find ownclass in tarObj fields
for (Field tarObjField : tarRu.getFields()) {
if (!tarObjField.getType().getName().equals(ownClass.getName()))
continue;
Method tarObjFieldGetter = tarRu.getGetter(tarObjField.getName());
if (tarObjFieldGetter == null)
continue;
ManyToOne manyToOne = tarObjField.getAnnotation(ManyToOne.class);
if (manyToOne == null)
manyToOne = tarObjFieldGetter.getAnnotation(ManyToOne.class);
if (manyToOne == null)
continue;
String fromRefCol = null;
JoinColumn joinCol = tarObjField.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");
String fromRefVal = null;
Object _obj = fromRefFieldGetter.invoke(t);
if (_obj != null)
fromRefVal = String.valueOf(_obj);
DAOFactory.getDeleteDAO(dsName).deleteByFieldIsValue(tarClass, new String[] { tarObjField.getName() }, new String[] { fromRefVal });
break;
}
}
if (joinTable != null) {
JoinColumn[] froms = joinTable.joinColumns();
if (froms == null || froms.length == 0)
continue;
String from = froms[0].name();
JoinColumn[] tos = joinTable.inverseJoinColumns();
if (tos == null || tos.length == 0)
continue;
String to = tos[0].name();
String relTable = joinTable.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;
String fromRefVal = String.valueOf(_obj);
String toRefCol = tos[0].referencedColumnName();
if (toRefCol == null || toRefCol.trim().length() == 0)
toRefCol = ORMConfigBeanUtil.getIdColumn(tarClass);
String toRefField = ORMConfigBeanUtil.getField(tarClass, toRefCol);
Method toRefFieldGetter = tarRu.getGetter(toRefField);
if (toRefFieldGetter == null)
throw new Exception("can not find the 'to ref field -> " + toRefField + "' of " + tarClass + " 's getter method");
Object _obj2 = toRefFieldGetter.invoke(tarObj);
if (_obj2 == null)
continue;
String toRefVal = String.valueOf(_obj2);
// delete from relTable where from = ? and to = ? ;
String format = "delete from %s where %s = ? and %s = ? ;";
String sql = String.format(format, relTable, from, to);
// finished
DAOFactory.getUpdateDAO(dsName).updateBySQLWithArgs(sql, fromRefVal, toRefVal);
}
}
}
}
}
}
});
}
use of org.eweb4j.orm.jdbc.transaction.Trans in project eweb4j-framework by laiweiwei.
the class Db method forceCreateTable.
public static void forceCreateTable(final Class<?> entity) {
String script = Model2Table.generateOne(entity, true, false);
final String[] sqls = script.split(";");
Transaction.execute(new Trans() {
public void run(Object... args) throws Exception {
for (String sql : sqls) {
if (sql == null || sql.trim().length() == 0)
continue;
Db.ar(entity).dao().sql(sql).execute();
}
}
});
}
use of org.eweb4j.orm.jdbc.transaction.Trans in project eweb4j-framework by laiweiwei.
the class Db method createTableIfNotExist.
public static void createTableIfNotExist(final Class<?> entity) {
String script = Model2Table.generateOne(entity, false, false);
final String[] sqls = script.split(";");
Transaction.execute(new Trans() {
public void run(Object... args) throws Exception {
for (String sql : sqls) {
if (sql == null || sql.trim().length() == 0)
continue;
Db.ar(entity).dao().sql(sql).execute();
}
}
});
}
use of org.eweb4j.orm.jdbc.transaction.Trans in project eweb4j-framework by laiweiwei.
the class EWeb4JConfig method _start.
private static String _start(String error, ConfigBean cb) {
if (cb == null) {
error = " can not read any configuration info! But now have bean repaired, please restart.";
} else {
StringBuilder infos = new StringBuilder("EWeb4JConfig.start \n");
infos.append("start-config-xml-path --> ").append(ConfigConstant.START_FILE_PATH()).append("\n");
infos.append("${RootPath} --> ").append(ConfigConstant.ROOT_PATH).append("\n");
infos.append(cb).append("\n");
log.debug(infos.toString());
// 检查配置信息格式是否填写正确
String error1 = CheckConfigBean.checkEWeb4JConfigBean(cb);
if (error1 != null)
error = error1;
String error2 = CheckConfigBean.checkEWeb4JIOCPart(cb.getIoc());
if (error2 != null)
if (error == null)
error = error2;
else
error += error2;
String error3 = CheckConfigBean.checkIOCXml(cb.getIoc().getIocXmlFiles());
if (error3 != null)
if (error == null)
error = error3;
else
error += error3;
String error4 = CheckConfigBean.checkEWeb4JORMPart(cb.getOrm());
if (error4 != null)
if (error == null)
error = error4;
else
error += error4;
String error5 = CheckConfigBean.checkORMXml(cb.getOrm().getOrmXmlFiles());
if (error5 != null)
if (error == null)
error = error5;
else
error += error5;
String error6 = CheckConfigBean.checkEWeb4JMVCPart(cb.getMvc());
if (error6 != null)
if (error == null)
error = error6;
else
error += error6;
String error7 = CheckConfigBean.checkMVCActionXmlFile(cb.getMvc().getActionXmlFiles());
if (error7 != null)
if (error == null)
error = error7;
else
error += error7;
String error8 = CheckConfigBean.checkInter(cb.getMvc().getInterXmlFiles());
if (error8 != null)
if (error == null)
error = error8;
else
error += error8;
if (error == null) {
// 验证通过,将读取到的信息放入缓存池中
SingleBeanCache.add(ConfigBean.class.getName(), cb);
// ------log-------
String info = "EWeb4J start configuration info have bean validated and pushed to the cache. ";
log.debug(info);
// ------log-------
// 继续验证其他组件配置信息
// properties
String error13 = null;
try {
for (org.eweb4j.config.bean.Prop f : cb.getProperties().getFile()) {
error13 = Props.readProperties(f, true);
if (error13 != null)
if (error == null)
error = error13;
else
error += error13;
}
} catch (Throwable e) {
log.warn(e.toString(), e);
if (error == null)
error = e.toString();
else
error += e.toString();
}
if (error == null)
log.debug("properties module -> ok");
if (error == null && ("true".equals(cb.getIoc().getOpen()) || "1".equals(cb.getIoc().getOpen()))) {
String error10 = IOCConfig.check();
if (error10 != null)
error = error10;
if (error == null)
log.debug("ioc module -> ok");
}
if (error == null && ("true".equals(cb.getOrm().getOpen()) || "1".equals(cb.getOrm().getOpen()))) {
// check the db connection
String error14 = DAOConfig.check();
if (error14 != null)
error = error14;
if (error == null) {
log.debug("orm.dao module -> ok");
// read jpa annotation
String error10 = new PojoAnnotationConfig().readAnnotation(cb.getOrm().getScanPojoPackage().getPath());
if (error10 != null)
error = error10;
if (error == null) {
log.debug("orm.pojo.annotation module -> ok");
// read orm xml file
String error11 = ORMConfig.check();
if (error11 != null)
error = error11;
if (error == null)
log.debug("orm.pojo.xml module -> ok");
// generate ddl
if (error == null && ("true".equals(cb.getOrm().getDdl().getGenerate()) || "1".equals(cb.getOrm().getDdl().getGenerate()))) {
log.debug("ddl.generate -> true");
DBInfoConfigBean dcb = DBInfoConfigBeanCache.get(cb.getOrm().getDdl().getDs());
if (DBType.MYSQL_DB.equals(dcb.getDataBaseType())) {
File sqlFile = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
if ("1".equals(cb.getOrm().getDdl().getOverride()) || "true".equals(cb.getOrm().getDdl().getOverride()) || !sqlFile.exists()) {
String sql = Model2Table.generate();
if (sql == null) {
error = "can not generate ddl file";
} else
log.debug("ddl.generate execute success -> " + sqlFile.getAbsolutePath());
} else {
log.warn("ddl.generate do not need to execute ->" + sqlFile.getAbsolutePath() + " is exists !");
}
} else {
log.warn("sorry only mysql db can use the ddl feature !");
}
if (error == null)
log.debug("orm.ddl.generate module -> ok");
}
// run ddl
if (error == null && ("true".equals(cb.getOrm().getDdl().getRun()) || "1".equals(cb.getOrm().getDdl().getRun()))) {
log.debug("ddl.run -> true");
DBInfoConfigBean dcb = DBInfoConfigBeanCache.get(cb.getOrm().getDdl().getDs());
if (dcb != null) {
if (DBType.MYSQL_DB.equals(dcb.getDataBaseType())) {
File sqlFile = new File(ConfigConstant.CONFIG_BASE_PATH() + cb.getOrm().getDdl().getDs() + "-create.sql");
StringBuilder builder = new StringBuilder();
BufferedReader sql_reader = null;
try {
sql_reader = new BufferedReader(new InputStreamReader(new FileInputStream(sqlFile)));
String line = null;
while ((line = sql_reader.readLine()) != null) {
builder.append(line);
}
final String[] sqls = builder.toString().split(";");
final UpdateDAO dao = DAOFactory.getUpdateDAO(cb.getOrm().getDdl().getDs());
Transaction.execute(new Trans() {
public void run(Object... args) throws Exception {
for (String sql : sqls) {
log.debug("ddl run -> " + sql);
dao.updateBySQL(sql);
}
}
});
} catch (Exception e) {
String _error13 = CommonUtil.getExceptionString(e);
if (_error13 != null)
error = _error13;
} finally {
if (sql_reader != null) {
try {
sql_reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
log.warn("sorry only mysql db can use the ddl feature !");
}
} else {
log.error("ddl.ds -> " + cb.getOrm().getDdl().getDs() + " not found !");
}
if (error == null)
log.debug("orm.ddl.run module -> ok");
}
//end orm.ddl.run
}
// end if error == null -> pojo.annotation
}
if (error == null)
log.debug("orm module -> ok ");
}
if (error == null && ("true".equals(cb.getMvc().getOpen()) || "1".equals(cb.getMvc().getOpen()))) {
String error20 = new ActionAnnotationConfig().readAnnotation(cb.getMvc().getScanActionPackage().getPath());
if (error20 != null)
error += error20;
if (error == null) {
log.debug("mvc.action.annotation module -> ok");
String error11 = ActionConfig.check();
if (error11 != null)
error = error11;
if (error == null) {
log.debug("mvc.action.xml module -> ok");
String error12 = new InterceptorAnnotationConfig().readAnnotation(cb.getMvc().getScanInterceptorPackage().getPath());
if (error12 != null)
error = error12;
if (error == null) {
log.debug("mvc.action.interceptor.annotation module -> ok");
String error21 = InterceptorConfig.check();
if (error21 != null)
error = error21;
if (error == null)
log.debug("mvc.action.interceptor.xml module -> ok");
}
// end if interceptor annotation ok
}
// end if action xml ok
}
if (error == null)
log.debug("mvc module -> ok");
}
// end mvc module
//CallBack after startup
Listeners listeners = cb.getListeners();
if (listeners != null && listeners.getListener() != null && !listeners.getListener().isEmpty()) {
for (ListenerBean lb : listeners.getListener()) {
String clazz = lb.getClazz();
try {
EWeb4JListener listener = (EWeb4JListener) CommonUtil.loadClass(clazz).newInstance();
listener.onStartup();
log.debug("listener->" + listener + ".onStartup execute...");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
// end if error == null
}
return error;
}
use of org.eweb4j.orm.jdbc.transaction.Trans in project eweb4j-framework by laiweiwei.
the class TestService method testTrans.
// 测试事务
public String testTrans(Pet _pet) {
String error = null;
// 事务模板
Transaction.execute(new Trans() {
@Override
public void run(Object... args) throws Exception {
// some eweb4j dao
}
}, 1, 2);
List<Pet> pets;
pets = DAOFactory.getSelectDAO().selectAll(Pet.class);
if (pets != null) {
}
return error;
}
Aggregations