春天 3 + Hibernate:我应该使用脏检查吗?如何使用基于注释的事务来做到这一点?
首先,我是 Spring 的新手,我不太了解它如何处理 Hibernate 事务,所以请随意教我一两件事! :D
我正在编写一个具有标准控制器、服务、数据访问和持久层的应用程序。所以我有例如 FileController、FileService、FileDao,以及处理 Hibernate 的 SpringFramework。
@Service
public class FileService {
@Autowired
FileDao fileDao;
public FileService() {}
@Transactional
public File getRootFile() {
return fileDao.getRootFile();
}
@Transactional
public File getById(long id) {
return fileDao.getById(id);
}
@Transactional
public void save(File file) {
fileDao.save(file);
}
}
我还将 OpenSessionInView 模式与 OpenSessionInViewInterceptor 结合使用。
我有两个问题:
- 我应该在视图中的打开会话中使用脏检查吗?这是否会导致视图可能对模型所做的任何更改被持久化?
- 如果脏检查是个好主意,我该怎么做?看来现在,我必须进行
save()
或update()
调用,否则脏对象在我的控制器返回后不会保留。 提前致谢!
First off, I'm new to Spring and I don't have my head quite wrapped around how it handles Hibernate transactions, so feel free to teach me a thing or two about it! :D
I'm writing an application with a standard Controller, Service, Data Access, and Persistence layer. So I have e.g. FileController, FileService, FileDao, with the SpringFramework handling Hibernate.
@Service
public class FileService {
@Autowired
FileDao fileDao;
public FileService() {}
@Transactional
public File getRootFile() {
return fileDao.getRootFile();
}
@Transactional
public File getById(long id) {
return fileDao.getById(id);
}
@Transactional
public void save(File file) {
fileDao.save(file);
}
}
I'm also using the OpenSessionInView pattern with an OpenSessionInViewInterceptor.
I have two questions:
- Should I be using dirty checking with an open session in the View? Would that cause any changes the View could potentially make to the Model to be persisted?
- If dirty checking is a good idea, how do I do it? It seems that right now, I have to make a
save()
orupdate()
call, otherwise dirty objects aren't persisted after my controller returns.Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您指的是乐观锁定,请查看 Hibernate 文档:
第 11.3 章。乐观并发控制
或JPA http://blogs.oracle.com/carolmcdonald/entry/ jpa_2_0_concurrency_and
您需要的注释是
@Version
If you mean optimistic locking, than have a look at Hibernate Documentation:
Chapter 11.3. Optimistic concurrency control
Or JPA http://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
The annotation you need is
@Version