我正在使用 Spring MVC + Hibernate
//Class for Generic Methods for **save and update**
@Service("PersistenceTemplate")
@Transactional
public class PersistenceTemplate {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
// SAVE
public <T> long save(T entity) throws DataAccessException {
Session session = sessionFactory.getCurrentSession();
long getGenVal=(Long) session.save(entity);
return getGenVal;
}
//UPDATE
public <T> void update(T entity) throws DataAccessException {
sessionFactory.getCurrentSession().update(entity);
}
}
AT Controller
@Resource(name = "PersistenceTemplate")
private PersistenceTemplate pt;
long result=pt.save(receiveTrxObj1);
pt.Update(receiveTrxObj2);
问题陈述
如果Update未能更新数据库中的实体,如何回滚save语句?
最佳答案
您可以使用应用程序级别异常来回滚实体操作。当这个自定义异常抛出时相关操作回滚。请参阅以下文档了解如何定义 custom rollback在 Spring 。
关于java - 手动回滚 Spring MVC + Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18447418/