【发布时间】:2011-12-19 23:45:11
【问题描述】:
我有这种非标准情况,我需要更新只读休眠事务中的对象。问题是,我加载了许多其他对象,在该事务中更改它们,但只有一个应该被保存,其余的回滚。我工作的数据库是 Postgresql 8.4
我不确定我对文档的理解是否正确,但提出了一种服务方法:
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
应该导致为该保存显式打开新的 RW 事务。
主要的是,这不起作用:
@Transactional(readOnly = true)
public class PersonService {
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
public void add(Person person){
person.setFirstName("changed its name");
save(person);
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void save(Person person) {
Session session = sessionFactory.getCurrentSession();
session.save(person);
session.flush();
}
}
然后我得到
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
从控制器调用“添加”方法。 'add' 处于只读模式,它将 Person 对象传递给 'save' 方法,该方法应该打开新的 Transaction 并保存到 DB。在我的“添加”方法的项目中,我加载了许多其他“人”对象,不应保存。 我需要只读方法的原因是我在代码中加载了更多的对象:) 回滚它们是我想做的最后一件事。
这样可以吗?
【问题讨论】:
标签: hibernate spring postgresql