日期:2014-05-19  浏览次数:20850 次

hibernate多表更新的问题
请问两张没有关联的表,在ssh中一个事务怎么处理?


------解决方案--------------------
一个事物中可以执行两条update语句
------解决方案--------------------
是啊 可以在一个事务里执行几次数据库操作。再提交事务
------解决方案--------------------
开始事务
session.beginTransaction(); 
提交事务
session.getTransaction().commit();
回滚
session.getTransaction().rollback();


------解决方案--------------------
这有什么关系了
public String doSave(Credituser dto) {
String msg = "";
// TODO Auto-generated method stub
session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
if (dto.getCreateperson().length() > 5) {
dto.setCurcreditamount(dto.getCreditlimit());
session.save(dto);
session.flush();
Creditadjustlog adjust = new Creditadjustlog();
adjust.setAdjperson(dto.getCreateperson());
adjust.setAdjtime(dto.getCreatetime());
adjust.setCreditamount("0");
adjust.setCuramount("0");
adjust.setWarnamount("0");
adjust.setExpiretime("");
adjust.setCreditseq(dto.getCreditseq().toString());
session.save(adjust);
session.flush();
String sql01 = "insert into creditadjustlog(adjustseq,adjtime,adjperson,creditamount,curamount,warnamount,expiretime,creditseq)"
+ " select SEQ_ADJUST_ID.NEXTVAL,'"
+ dto.getCreatetime()
+ "','"
+ dto.getCreateperson()
+ "',curcreditamount,creditlimit,warnamount,expiretime,creditseq from credituser where credituserno='"
+ dto.getCreateperson() + "'";
session.createSQLQuery(sql01).executeUpdate();
session.flush();
String sql02 = "update credituser set creditlimit=to_char(to_number(creditlimit)-"
+ Integer.parseInt(dto.getCreditlimit())
+ ") where credituserno='"
+ dto.getCreateperson()
+ "'";
session.createSQLQuery(sql02).executeUpdate();
session.flush();
} else {
dto.setCurcreditamount(dto.getCreditlimit());
session.save(dto);
session.flush();
Creditadjustlog adjust = new Creditadjustlog();
adjust.setAdjperson(dto.getCreateperson());
adjust.setAdjtime(dto.getCreatetime());
adjust.setCreditamount("0");
adjust.setCuramount("0");
adjust.setWarnamount("0");
adjust.setExpiretime("");
adjust.setCreditseq(dto.getCreditseq().toString());
session.save(adjust);
session.flush();
}

ts.commit();
msg = "0";
} catch (Exception e) {
System.out.println("insert error: " + e.getMessage());
if (ts != null) {
ts.rollback();
}
msg = "error";
} finally {
HibernateSessionFactory.closeSession();
}
return msg;
}