Spring Orm 简明教程

Spring ORM - Overview

Spring 框架与 Hibernate、Java 持久化 API (JPA)、Java 数据对象 (JDO) 和 iBATIS SQL 映射等 ORM 框架很好地集成。Spring 提供资源管理、数据访问对象 (DAO) 实现和事务策略。Spring 允许通过依赖项管理配置 ORM 库功能。Spring 为其支持的所有 ORM 库维护统一的 DAO 异常层次结构和通用的事务管理。

The Spring Framework integrates well with ORM frameworks like Hibernate, Java Persistence API (JPA), Java Data Objects (JDO) and iBATIS SQL Maps. Spring provides resource management, data access object (DAO) implementations, and transaction strategies. Spring allows to configure ORM library features through dependency management. Spring maintains a uniform DAO Exception hiearchies and a generic transaction management for all the ORM libraries it supports.

Spring IoC 容器促进了 ORM 配置和轻松部署。以下是使用 Spring 框架创建 ORM DAO 的主要优点。

Spring IoC container facilitates ORM configurations and easy deployment. Following are the key benefits of using Spring framework to create ORM DAO.

  1. Easy to Test − Using spring IoC, an ORM implementation can be easily configured. Each piece of persistence unit can be tested in isolation.

  2. Common Data Access Exception − Spring wraps ORM Tools exceptions to a common runtime exception as DataAccessException. This approach helps to handle most persistence exception (non-recoverable) in appropriate layers. No need to handle ORM specific boilerplate catch/throws/exception declarations.

  3. General Resource Management − Spring application contexts manages persistence objects, their configurations easily. For example, Hibernate SessionFactory instances, JPA EntityManagerFactory instances, JDBC DataSource instances, iBatis SQL Maps configuration objects and other related objects. Spring handles the local as well JTA transaction management by itself.

  4. Integrated transaction management − Spring AOP can be used to wrap an ORM code with a declarative AOP styled interceptor either using @Transaction annotation or by specifying transaction AOP advice in XML configuration file. Spring handles transaction semantics, exception handling, rollback and so on. Spring allows to swap transaction managers without affecting the ORM code.