ORM Framework and the patterns used

Spring provides support for several ORM Frameworks, such as Hibernate, the Java Persistence API (JPA), iBATIS, and Java Data Objects (JDO). By using any ORM solution in your application, you can easily persist and access data in the form of POJO objects from relational databases. The Spring ORM module is an extension of the previously discussed Spring JDBC DAO module. Spring provides ORM templates, such as JDBC-based templates, to work in the integration tier or data access layer. The following are the ORM Frameworks and integration supported by the Spring Framework:

  • Hibernate
  • Java Persistence API
  • Java Data Objects
  • iBATIS
  • Data access object implementations
  • Transaction strategies

You can use Spring's dependency injection feature to configure ORM solutions in your application. Spring also adds important enhancements to the ORM layer in your data access applications. The following are the benefits of using the Spring Framework to create your ORM DAOs:

  • Easier development and testing: Spring's IoC container manages the beans for ORM DAOs. You can easily swap the implementation of the DAO interface by using Spring's dependency injection feature. It also makes it easy to test persistence-related code in isolation.
  • Common data access exceptions: Spring provides a consistent data exception hierarchy to handle exceptions at the persistence layer. It wraps all the checked exceptions from the ORM tool, and converts these exceptions to unchecked general exceptions which are not related to any specific ORM solution and are DB-specific.
  • General resource management: Resources such as DataSource, DB connections, Hibernates SessionFactory, JPA EntityManagerFactory, and others are managed by the Spring IoC container. Spring also manages transactions--local or global--using JTA.
  • Integrated transaction management: Spring provides declarative and programmatic transaction management in your application. For declarative transaction management, you can use the @Transactional annotation.

The main approach to Spring's integration with the ORM solution is loose coupling between the application's layers; that is, the business layer and the data access layer. It is clear application layering, and is independent of any specific database and transaction technology. Business services in the application are no longer dependent on data access and a specific transaction strategy. Because Spring manages the resources used in the integration layer, you don't need to look up resources for specific data access technologies. Spring provides templates for the ORM solution to remove the boilerplate codes, and it provides a consistent approach across all ORM solutions.

In Chapter 7, Accessing Database with Spring and JDBC Template Patterns, you saw how Spring solves two major problems of the integration layer in the application. The first problem was redundant code for managing resources from the application, and the second problem was handling checked exceptions in the application at development time. Similarly, the Spring ORM module also provides solutions to these two problems, as we'll discuss in the following sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset