Hibernate 使用session 進行事務處理
阿新 • • 發佈:2019-02-08
public void saveLogoPath(String path, List<Terminal> temList)
throws Exception {
Session session = null;
Transaction trans = null;
// SqlHelper sp = new SqlHelper();
try {
session = getSessionFactory().openSession();
trans = session.beginTransaction();
// String sql =
// "insert into t_upgrade(upgradeFile,status,remark)values(?,?,?)";
if (path.indexOf("\\webapps\\ebook_web\\") > 0) {
path = path.substring(path.indexOf("\\webapps\\ebook_web\\")
+ 1 + "\\webapps\\ebook_web\\".length());
} else {
throw new RuntimeException(" upload soft path is error");
}
//
// session.createSQLQuery(sql).setParameter(0, path).setParameter(1,
// "1").setParameter(2, "公司logo").executeUpdate();
// sp.update(sql, params);
// String fileName = path.substring(path.lastIndexOf("\\") + 1);
String fileName = path;
if (temList != null && temList.size() > 0) {
for (Terminal t : temList) {
String sql2 = "insert into t_term_task(TERM_NO,TASK_TYPE,FILE_NAME,FINISH_FLAG,EXT_FINISH_FLAG,STATUS,TASK_STATE,CREATE_TIME,MODIFY_TIME)values(?,?,?,?,?,?,?,?,? )";
// 無論是通過不同型別引數的設定介面來設定SQL引數,還是通過setParameter來設定引數,下標都是從0開始的,而不是從1開始的!
session.createSQLQuery(sql2).setParameter(0,
t.getTerminal_no()).setParameter(1,
Constant.TASK_TYPE_LOGO_UPDATE).setParameter(2,
fileName).setParameter(3,
Constant.TERM_TASK_FINISH_FLAG_NOT_YET)
.setParameter(4,
Constant.TERM_TASK_EXT_FINISH_FLAG_NOT_YET)
.setParameter(5, Constant.STATUS_USEFUL)
.setParameter(6, Constant.STATUS_USEFUL)
.setParameter(7,
String.valueOf(new Date().getTime()))
.setParameter(8,
String.valueOf(new Date().getTime()))
.executeUpdate();
System.out.println("id:" + t.getId() + "termNO::"
+ t.getTerminal_no());
// sp.update(sql2, params2);
}
}
trans.commit();
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
throws Exception {
Session session = null;
Transaction trans = null;
// SqlHelper sp = new SqlHelper();
try {
session = getSessionFactory().openSession();
trans = session.beginTransaction();
// String sql =
// "insert into t_upgrade(upgradeFile,status,remark)values(?,?,?)";
if (path.indexOf("\\webapps\\ebook_web\\") > 0) {
path = path.substring(path.indexOf("\\webapps\\ebook_web\\")
+ 1 + "\\webapps\\ebook_web\\".length());
} else {
throw new RuntimeException(" upload soft path is error");
}
//
// session.createSQLQuery(sql).setParameter(0, path).setParameter(1,
// "1").setParameter(2, "公司logo").executeUpdate();
// sp.update(sql, params);
// String fileName = path.substring(path.lastIndexOf("\\") + 1);
String fileName = path;
if (temList != null && temList.size() > 0) {
for (Terminal t : temList) {
String sql2 = "insert into t_term_task(TERM_NO,TASK_TYPE,FILE_NAME,FINISH_FLAG,EXT_FINISH_FLAG,STATUS,TASK_STATE,CREATE_TIME,MODIFY_TIME)values(?,?,?,?,?,?,?,?,? )";
// 無論是通過不同型別引數的設定介面來設定SQL引數,還是通過setParameter來設定引數,下標都是從0開始的,而不是從1開始的!
session.createSQLQuery(sql2).setParameter(0,
t.getTerminal_no()).setParameter(1,
Constant.TASK_TYPE_LOGO_UPDATE).setParameter(2,
fileName).setParameter(3,
Constant.TERM_TASK_FINISH_FLAG_NOT_YET)
.setParameter(4,
Constant.TERM_TASK_EXT_FINISH_FLAG_NOT_YET)
.setParameter(5, Constant.STATUS_USEFUL)
.setParameter(6, Constant.STATUS_USEFUL)
.setParameter(7,
String.valueOf(new Date().getTime()))
.setParameter(8,
String.valueOf(new Date().getTime()))
.executeUpdate();
System.out.println("id:" + t.getId() + "termNO::"
+ t.getTerminal_no());
// sp.update(sql2, params2);
}
}
trans.commit();
} catch (Exception e) {
trans.rollback();
e.printStackTrace();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
session.createSQLQuery("select * from t_config").list();