问题描述
在使用Hibernate作为持久层框架时,我们有时会使用 createNativeQuery 方法编写原生SQL来更新数据:
public boolean updateData(UUID newAddCardId){
String hql = "update fdw_detail set IS_DELETED = 2 where INV_DET_ID = :bookId";
try {
template.execute(session -> session.createNativeQuery(hql)
.setParameter("bookId", bookId)
.executeUpdate()
);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
但是在使用过程中可能会遇到事务不生效,更新数据不提交的问题!
问题分析
我这边是这样使用的 :
Service 层:
@Service
public class HService {
@Transactional
public String updateTest(UUID newAddCardId) {
//调用真正更新数据库的方法
updateData(newAddCardId);
}
public boolean updateData(UU