Hibernate - 想要将删除和存储放在一个“事务”中,如果发生pb,则所有事务都将回滚
我有一种方法可以删除一些项目,然后插入一些其他项目。
public void refresh() {
if (newitems != null) {
toto.clear();
for (totoDao p : newItems) {
toto.store(p);
}
}
newitems = null;
}
public void clear() {
final Session session = this.getHibernateUtil().getSession();
int n = session.createQuery(DELETE).executeUpdate();
session.clear();
session.flush();
}
public void store(TotoDao object) {
final Session session = this.getHibernateUtil().getSession();
session.saveOrUpdate(object);
session.flush();
}
目前,我在clear()方法中有一个刷新,在store()方法中有另一个刷新。
我想将所有这些内容添加到一个“事务”中,如果出现某些情况,应用程序会在 toto.clear() 之后重新启动,例如,我希望该事务回滚所有块。
那么,实现性能和持久性的最佳解决方案是什么?
谢谢 !
I have a method which delete some items, and next insert some others items.
public void refresh() {
if (newitems != null) {
toto.clear();
for (totoDao p : newItems) {
toto.store(p);
}
}
newitems = null;
}
public void clear() {
final Session session = this.getHibernateUtil().getSession();
int n = session.createQuery(DELETE).executeUpdate();
session.clear();
session.flush();
}
public void store(TotoDao object) {
final Session session = this.getHibernateUtil().getSession();
session.saveOrUpdate(object);
session.flush();
}
For the moment I have one flush in clear() method and other one in store() method.
I want to add all of theses stuffs in one "transaction", if somethings appears, a application restart just after the toto.clear(), for example, i want that transaction rollback all the block.
So what are the best solution for perfomances and persistances ?
Thx !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将这两个方法调用包含在一个唯一的事务中即可。
冲洗与交易没有任何关系。它只是意味着“真正执行持久保存会话中所做的修改所需的所有 SQL 语句”。但提交只会在事务结束时完成。
手动刷新会话几乎总是不必要的。必要时让 Hibernate 来做。
另请注意,DAO 应该是一个允许查询和更新实体的服务对象。它不应该是一个持久的实体。
阅读 http://docs.jboss.org/hibernate /core/3.6/reference/en-US/html_single/#transactions 和 http://docs.jboss.org/hibernate/ core/3.6/reference/en-US/html_single/#objectstate-flushing
Just enclose these two method calls inside a unique transaction.
Flushing doesn't have anything to do with transactions. It just means "really execute all the SQL statements needed to persist the modifications made in the session". But the commit will only be done at the end of the transaction.
Flushing the session manually is almost always unnecessary. Let Hibernate do it when it has to.
Also, note that a DAO is supposed to be a service object allowing to query and update entities. It's not supposed to be a persistent entity.
Read http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#transactions and http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-flushing
Spring 为事务管理提供了很好的解决方案。
在此页面上,您将找到使用 XML 文件配置 spring/hibernate 的方法。
如果您需要一些解释,请询问,我会尽快帮助您。
一些例子:
Spring has good solutions for transaction management.
On this page you'll find the way to configure spring/hibernate with XML files.
If you need some explanation, just ask and i'll try to help you asap.
Some exemple: