首先,我是 Spring 的新手,我不太了解它如何处理 Hibernate 事务,所以请随意教我一两件事! :D
我正在编写一个具有标准 Controller 、服务、数据访问和持久层的应用程序。所以我有例如FileController、FileService、FileDao,以及处理 Hibernate 的 SpringFramework。
<前>@Service
公共(public)类文件服务{
@Autowired
FileDao 文件Dao;
公共(public)文件服务(){}
@事务性
公共(public)文件 getRootFile() {
返回 fileDao.getRootFile();
}
@事务性
公共(public)文件 getById(长 id) {
返回 fileDao.getById(id);
}
@事务性
公共(public)无效保存(文件文件){
fileDao.save(文件);
}
}
我还将 OpenSessionInView 模式与 OpenSessionInViewInterceptor 结合使用。
我有两个问题:
- 我应该在 View 中的打开 session 中使用脏检查吗?这是否会导致 View 可能对模型所做的任何更改被持久化?
- 如果脏检查是个好主意,我该怎么做?看来现在,我必须进行
save()
或update()
调用,否则脏对象在我的 Controller 返回后不会保留。 提前致谢!
最佳答案
如果您指的是乐观锁定,请查看 Hibernate 文档: Chapter 11.3. Optimistic concurrency control
或 JPA http://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
您需要的注释是@Version
@Version
@Column(name = "version", nullable = false, length = 5)
public int getVersion() { ... }
关于java - Spring 3 + Hibernate : Should I use dirty checking, 以及如何使用基于注释的事务来做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5573176/