Introduction to ORM with Spring
Spring Framework 1 支持与 Java 持久 API (JPA) 集成,并支持本地 Hibernate 进行资源管理、数据访问对象 (DAO) 实现和事务策略。例如,对于 Hibernate,它享有头等公民支持,并具有多个方便的 IoC 特性,这些特性解决了众多典型的 Hibernate 集成问题。你可以通过依赖注入配置所有受支持的 OR(对象关系)映射工具的特性。它们可以参与 Spring 的资源和事务管理,并且遵守 Spring 的通用事务和 DAO 异常层次结构。建议的集成风格是根据纯 Hibernate 或 JPA API 编写 DAO。
The Spring Framework supports integration with the Java Persistence API (JPA) and supports native Hibernate for resource management, data access object (DAO) implementations, and transaction strategies. For example, for Hibernate, there is first-class support with several convenient IoC features that address many typical Hibernate integration issues. You can configure all of the supported features for OR (object relational) mapping tools through Dependency Injection. They can participate in Spring’s resource and transaction management, and they comply with Spring’s generic transaction and DAO exception hierarchies. The recommended integration style is to code DAOs against plain Hibernate or JPA APIs.
当你创建数据访问程序时,Spring 会显著增强你选择中的 ORM 层。你可以根据需要利用大量集成支持,并且你应该将这个集成工作与内部构建类似基础设施的成本和风险进行比较。你可以将大量 ORM 支持用作库,无论采用什么技术,因为所有内容都设计为一组可重用的 JavaBean。Spring IoC 容器中的 ORM 方便了配置和部署。因此,本节中的大多数示例都显示了 Spring 容器内的配置。
Spring adds significant enhancements to the ORM layer of your choice when you create data access applications. You can leverage as much of the integration support as you wish, and you should compare this integration effort with the cost and risk of building a similar infrastructure in-house. You can use much of the ORM support as you would a library, regardless of technology, because everything is designed as a set of reusable JavaBeans. ORM in a Spring IoC container facilitates configuration and deployment. Thus, most examples in this section show configuration inside a Spring container.
使用 Spring Framework 创建 ORM DAO 的好处包括:
The benefits of using the Spring Framework to create your ORM DAOs include:
-
Easier testing. Spring 的 IoC 方法可以很容易地交换 Hibernate
SessionFactory
实例、JDBCDataSource
实例、事务管理器和映射对象实现(如果需要)的实现和配置位置。而这反过来又使得更加容易单独测试每段与持久性相关的代码。 -
Easier testing. Spring’s IoC approach makes it easy to swap the implementations and configuration locations of Hibernate
SessionFactory
instances, JDBCDataSource
instances, transaction managers, and mapped object implementations (if needed). This in turn makes it much easier to test each piece of persistence-related code in isolation. -
Common data access exceptions. Spring 可以封装 ORM 工具抛出的异常,将它们从专有(可能是已检查)异常转换为一个通用的运行时
DataAccessException
层次结构。此功能允许您仅在适当的层中处理大多数不可恢复的持久性异常,而无需烦人的样板化 catch、throws 和异常声明。您仍然可以根据需要捕捉并处理异常。请记住,JDBC 异常(包括特定于数据库的方言)也会转换为相同的层次结构,这意味着您可以在一致编程模型中使用 JDBC 执行一些操作。 -
Common data access exceptions. Spring can wrap exceptions from your ORM tool, converting them from proprietary (potentially checked) exceptions to a common runtime
DataAccessException
hierarchy. This feature lets you handle most persistence exceptions, which are non-recoverable, only in the appropriate layers, without annoying boilerplate catches, throws, and exception declarations. You can still trap and handle exceptions as necessary. Remember that JDBC exceptions (including DB-specific dialects) are also converted to the same hierarchy, meaning that you can perform some operations with JDBC within a consistent programming model. -
General resource management. Spring 应用程序上下文可以处理 Hibernate
SessionFactory
实例、JPAEntityManagerFactory
实例、JDBCDataSource
实例和其他相关资源的位置和配置。这使得管理和更改这些值变得很容易。Spring 提供了持久性资源的高效、简单且安全的处理方法。例如,通常使用 Hibernate 的相关代码需要使用同一个 HibernateSession
来确保效率和正确的交易处理。Spring 可以轻松地创建Session
并将其透明地绑定到当前线程,通过 HibernateSessionFactory
公开当前Session
。因此,Spring 解决了许多典型的 Hibernate 使用中的长期问题,适用于任何本地或 JTA 交易环境。 -
General resource management. Spring application contexts can handle the location and configuration of Hibernate
SessionFactory
instances, JPAEntityManagerFactory
instances, JDBCDataSource
instances, and other related resources. This makes these values easy to manage and change. Spring offers efficient, easy, and safe handling of persistence resources. For example, related code that uses Hibernate generally needs to use the same HibernateSession
to ensure efficiency and proper transaction handling. Spring makes it easy to create and bind aSession
to the current thread transparently, by exposing a currentSession
through the HibernateSessionFactory
. Thus, Spring solves many chronic problems of typical Hibernate usage, for any local or JTA transaction environment. -
Integrated transaction management. 您可以通过
@Transactional
注释或在 XML 配置文件中明确配置事务 AOP 建议,使用声明性面向切面编程 (AOP) 样式方法拦截器,封送您的 ORM 代码。在这两种情况下,都会为您处理事务语义和异常处理(回滚等)。正如 Resource and Transaction Management 中讨论的那样,您还可以交换各种事务管理器,而不会影响您相关的 ORM 代码。例如,您可以在本地事务和 JTA 之间进行交换,并且在两种情况下都提供相同的完整服务(如声明性事务)。此外,与 JDBC 相关的代码可以与您用于执行 ORM 的代码完全集成。这对于不适合 ORM 的数据访问(如批量处理和 BLOB 流处理)很有用,但仍需要与 ORM 操作共享公共事务。 -
Integrated transaction management. You can wrap your ORM code with a declarative, aspect-oriented programming (AOP) style method interceptor either through the
@Transactional
annotation or by explicitly configuring the transaction AOP advice in an XML configuration file. In both cases, transaction semantics and exception handling (rollback and so on) are handled for you. As discussed in Resource and Transaction Management, you can also swap various transaction managers, without affecting your ORM-related code. For example, you can swap between local transactions and JTA, with the same full services (such as declarative transactions) available in both scenarios. Additionally, JDBC-related code can fully integrate transactionally with the code you use to do ORM. This is useful for data access that is not suitable for ORM (such as batch processing and BLOB streaming) but that still needs to share common transactions with ORM operations.
关于更全面的 ORM 支持,包括对 MongoDB 等替代数据库技术的支持,您可以查看 Spring Data 项目套件。如果您是 JPA 用户,则 [role="bare"][role="bare"]https://spring.io 中的 使用 JPA 访问数据的入门指南 提供了一个不错的介绍。 |
For more comprehensive ORM support, including support for alternative database technologies such as MongoDB, you might want to check out the Spring Data suite of projects. If you are a JPA user, the Getting Started Accessing Data with JPA guide from [role="bare"]https://spring.io provides a great introduction. |