java 使用 Springs 事务管理与使用 hibernate 的好处

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4940815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:42:20  来源:igfitidea点击:

Benefits of using Springs Transaction management vs using hibernate

javaspringhibernate

提问by John Baker

I've been trying to learn spring and hibernate, and I've used a lot of examples around the net to put together a nice application. However, I realized now that Spring supports transaction management. In my spring app I just simply made whatever calls I wanted to, directly to hibernate. Is there a reason/benefit as to why people would use Spring's transaction management/db stuff?

我一直在努力学习 spring 和 hibernate,我已经使用了很多网络上的例子来组合一个很好的应用程序。但是,我现在意识到 Spring 支持事务管理。在我的 spring 应用程序中,我只是简单地拨打我想要的任何电话,直接进入休眠状态。为什么人们会使用 Spring 的事务管理/db 的东西有什么原因/好处吗?

回答by axtavt

The real advantages are:

真正的优势是:

  • Lightweight declarative syntax. Compare:

    public void saveEmployee(Employee e) {
        Session s = sf.getCurrentSession();    
        s.getTransaction().begin();
        s.save(e);    
        s.getTransaction().commit();
    }
    

    and

    @Transactional
    public void saveEmployee(Employee e) {
        sf.getCurrentSession().save(e);
    }
    
  • Flexible transaction propagation. Imagine that now you need to execute this saveEmployee()method as a part of a complex transaction. With manual transaction management, you need to change the method since transaction management is hard-coded. With Spring, transaction propagation works smoothly:

    @Transactional
    public void hireEmployee(Employee e) {
        dao.saveEmployee(e);
        doOtherStuffInTheSameTransaction(e);
    }
    
  • Automatic rollback in the case of exceptions

  • 轻量级声明式语法。比较:

    public void saveEmployee(Employee e) {
        Session s = sf.getCurrentSession();    
        s.getTransaction().begin();
        s.save(e);    
        s.getTransaction().commit();
    }
    

    @Transactional
    public void saveEmployee(Employee e) {
        sf.getCurrentSession().save(e);
    }
    
  • 灵活的交易传播。想象一下,现在您需要将此saveEmployee()方法作为复杂事务的一部分来执行。使用手动事务管理,您需要更改方法,因为事务管理是硬编码的。使用 Spring,事务传播工作顺利:

    @Transactional
    public void hireEmployee(Employee e) {
        dao.saveEmployee(e);
        doOtherStuffInTheSameTransaction(e);
    }
    
  • 异常情况自动回滚

回答by Aravind Yarram

The spring's reference docmentions it very clear

春天的参考文档中提到说得很清楚

The Spring Framework provides a consistent abstraction for transaction management that delivers the following benefits:

  • Provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
  • Supports declarative transaction management.
  • Provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.
  • Integrates very well with Spring's various data access abstractions.

Spring 框架为事务管理提供了一致的抽象,具有以下优点:

  • 提供跨不同事务 API(例如 JTA、JDBC、Hibernate、JPA 和 JDO)的一致编程模型。
  • 支持声明式事务管理。
  • 为编程事务管理提供比许多复杂事务 API(如 JTA)更简单的 API。
  • 与 Spring 的各种数据访问抽象很好地集成。


Provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.

提供跨不同事务 API(例如 JTA、JDBC、Hibernate、JPA 和 JDO)的一致编程模型。

Let us say you are currently using hibernate api for transaction management and sometime down the road you would want to switch to JDO. This requires the change of transaction management code. If you use Spring then there is no change.

假设您目前正在使用 hibernate api 进行事务管理,并且有时您想切换到 JDO。这需要更改事务管理代码。如果您使用 Spring,则没有任何变化。

Supports declarative transaction management.

支持声明式事务管理。

Similar to EJB

类似于 EJB

Provides a simpler API for programmatic transaction management than a number of complex transactions APIs such as JTA.

为编程事务管理提供比许多复杂事务 API(如 JTA)更简单的 API。

JTA and JDBC have different APIs for transaction management. Spring abstracts that out by providing a uniform API.

JTA 和 JDBC 具有用于事务管理的不同 API。Spring 通过提供统一的 API 将其抽象出来。