Hibernate持久化方法新增事務
阿新 • • 發佈:2019-02-07
Hibernate一個方法如果 多次操作資料庫,如何新增事務管理,後臺java程式碼參考如下,注意註解@Transactional
import org.springframework.transaction.annotation.Transactional; /** * 更新t_exception_log表並新增記錄到T_EXCE_PROC_LOG表 */ @Transactional public voidupdateProcessRecord(String elogid, String nofified, String note,String loginId)throws Exception { List<Object> params = newArrayList<Object>(); StringBuffer hql = newStringBuffer(); hql.append(" updatet_exception_log l set l.notified= ? where l.id= ? "); params.add(Long.parseLong(nofified)); params.add(elogid); hibernateDao.updateBySql(hql.toString(),params.toArray()); //新增記錄到T_EXCE_PROC_LOG表 TExceptionLog exceptionLog =findTExceptionLogId(Long.parseLong(elogid)); TExceProcLog procLog = newTExceProcLog(); procLog.setTExceptionLog(exceptionLog); procLog.setCreatedBy(Long.parseLong(loginId)); procLog.setCreatedTime(newDate()); procLog.setStatus(Long.parseLong(nofified)); procLog.setDescr(note); hibernateDao.saveOrUpdate(procLog); }