Hibernate-希望将删除和存储放在一个“中”;交易";,如果发生pb,则将回滚所有

Hibernate-希望将删除和存储放在一个“中”;交易";,如果发生pb,则将回滚所有,hibernate,persistence,flush,Hibernate,Persistence,Flush,我有一个方法,删除一些项目,然后插入一些其他项目 public void refresh() { if (newitems != null) { toto.clear(); for (totoDao p : newItems) { toto.store(p); } } newitems = null; } public void clear() { final Session session

我有一个方法,删除一些项目,然后插入一些其他项目

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();
}
目前,我有一个flush in clear()方法和另一个in store()方法

我想在一个“事务”中添加所有这些内容,例如,如果出现某些内容,应用程序将在toto.clear()之后重新启动,我希望该事务回滚所有块

那么,性能和持久性的最佳解决方案是什么


谢谢

只需将这两个方法调用封装在一个唯一的事务中即可

刷新与事务无关。它只是意味着“真正执行所有需要持久化会话中所做修改的SQL语句”。但提交将仅在事务结束时完成

手动刷新会话几乎总是不必要的。必要时让Hibernate来做

另外,请注意,DAO应该是允许查询和更新实体的服务对象。它不应该是一个持久的实体


Read和

只需将这两个方法调用包含在一个唯一的事务中

刷新与事务无关。它只是意味着“真正执行所有需要持久化会话中所做修改的SQL语句”。但提交将仅在事务结束时完成

手动刷新会话几乎总是不必要的。必要时让Hibernate来做

另外,请注意,DAO应该是允许查询和更新实体的服务对象。它不应该是一个持久的实体


阅读和

Spring有很好的解决方案。 在本页中,您将找到使用XML文件配置spring/hibernate的方法

如果你需要解释,尽管问,我会尽快帮你

例如:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <aop:config>
    <aop:pointcut id="pointcutId" expression="execution(* com.stackoverflow.service.FooService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="get*" read-only="true"/>
      <!-- other methods (By example Rollback for NullPointerException)-->
      <tx:method name="*" read-only="false" rollback-for="NullPointerException"/>
    </tx:attributes>
  </tx:advice>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory" /> 
</bean>

 ...

</beans>

...

Spring有很好的解决方案。 在本页中,您将找到使用XML文件配置spring/hibernate的方法

如果你需要解释,尽管问,我会尽快帮你

例如:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <aop:config>
    <aop:pointcut id="pointcutId" expression="execution(* com.stackoverflow.service.FooService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="get*" read-only="true"/>
      <!-- other methods (By example Rollback for NullPointerException)-->
      <tx:method name="*" read-only="false" rollback-for="NullPointerException"/>
    </tx:attributes>
  </tx:advice>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
   <property name="sessionFactory" ref="sessionFactory" /> 
</bean>

 ...

</beans>

...

我们有transactionManager,我们不做一些beginTransaction()。在单例类HibernateUtil中,我们初始化sessionFactory。因此,问题是我不知道事务从何处开始和结束。事务在调用事务方法时开始,adn在该方法结束时提交或回滚,它由Spring使用AOP完成。方法是否是事务性的取决于Spring配置和@transactional注释。看我们有transactionManager,我们不做一些beginTransaction()。在单例类HibernateUtil中,我们初始化sessionFactory。因此,问题是我不知道事务从何处开始和结束。事务在调用事务方法时开始,adn在该方法结束时提交或回滚,它由Spring使用AOP完成。方法是否是事务性的取决于Spring配置和@transactional注释。您的解决方案没有回答有关回滚的问题。如果您的add/store/delete引发异常,会发生什么情况?谢谢,然后将其设为null。:)此外,未定义会话对象。我只是想展示一下这个想法。你的解决方案并没有回答有关回滚的问题。如果您的add/store/delete引发异常,会发生什么情况?谢谢,然后将其设为null。:)此外,未定义会话对象。我只是想展示一下这个想法。谢谢,但我已经用Springbean设置了一个带有事务注释的解决方案。唯一的想法是我问过我是否应该设置@Transaction传播的模式;)。您的问题不是:“那么性能和持久性的最佳解决方案是什么?”谢谢,但我已经用Springbean设置了一个带有事务注释的解决方案。唯一的想法是我问过我是否应该设置@Transaction传播的模式;)。你的问题不是:“那么什么是表现和坚持的最佳解决方案?”