日期:2014-05-20  浏览次数:20716 次

JPA Hibernate 事务问题
我在用JPA插入数据时,数据库中并没有及时更新。(JPA+JSF)/(Hibernate+JSF)
  只能通过
  getSession().beginTransaction().begin();
getSession().save(transientInstance);
getSession().beginTransaction().commit();
  个人觉得这样有些麻烦,JPA/Hibernate中是否有相关的机制,可以自动提交事务的,或者调协全局事务边界之类的东西?
   
  各位大虾,知道的请提示下,不知道的请交流下!
  谢谢了!

------解决方案--------------------
XML code

<!-- JPA Transaction manager :JPA事务控制设置-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <aop:config>
        <aop:pointcut id="crudMethods" expression="execution(* org.sshdemo.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="crudMethods"/>
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

------解决方案--------------------
hibernate底层其实还是用的JDBC,所以事务边界是少不了的。除非你用SPRING去做事务管理