Spring provides integration with various ORM tools available in the market such as Hibernate, JPA (Java Persistence API), JDO (Java Data Objects) and iBatis SQL maps. Spring framework supports various packages for O/R (Object Relational) mappers and complies with Spring’s generic transaction and Data Access Objects (DAO) exception hierarchies. There are usually two integration styles: either using Spring’s DAO ‘templates’ or coding DAOs against plain Hibernate, iBatis, etc. APIs. In both cases, DAOs can be configured through Dependency Injection and participate in Spring’s resource and transaction management.
Spring adds significant enhancements to the ORM layer of your choice when you create data access applications. ORM in a Spring IoC container facilitates configuration and deployment.
Benefits of using the Spring Framework to create your ORM DAOs include:
- Easier testing – Spring’s unique capability of ability to swap the implementations using Spring configuration enables it much easier to test each piece of persistence-related code in isolation.
- Common data access exceptions – Spring can wrap exceptions from the ORM, converting them from proprietary exceptions to a common runtime DataAccessException hierarchy. This feature allows you to handle most persistence exceptions. JDBC exceptions 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’s application context can handle almost all the configuration required using the various configuration styles available. This makes these values easy to manage and change at various stages in application development with ease.
- 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. You can also swap various transaction managers, without affecting your ORM-related code. In addition, JDBC-related code can fully integrate in a transactional way with the code you use to do ORM.
Covering Spring ORM integration completely is out of scope of this book, but since this very important for any modern day application, I have picked up one of the well known ORM tool namely Hibernate to explain its integration to the Spring framework to help you understand the nitty-gritty’s involved in such an integration.
Spring and Hibernate
Hibernate is a powerful technology for persisting data in any kind of application. Though Hibernate is not the only persistence solution, it has become very famous over the recent past because of its huge variety of features when compared with its competitors. It takes much of the database related boiler-plate code from the developers, thereby asking the developers to concentrate on the core business logic of the application and not with the error-prone SQL syntax.
Spring Data makes it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services as well as provide improved support for relational database technologies. Spring Data is an umbrella open source project which contains many subprojects that are specific to a given database.
The various projects under Spring Data are given below for your reference:
|Relational Databases||JPA||Simplifies the development of creating a JPA-based data access layer|
|Relational Databases||JDBC Extensions||Support for Oracle RAC, Advanced Queuing, and Advanced datatypes.|
|Big Data||Apache Hadoop||Support for open source Apache Hadoop project. It allows to realize reliable, scalable, distributed computing and data storage.|
|Data-Grid||GemFire||VMware vFabric GemFire is a distributed data management platform providing dynamic scalability, high performance, and database-like persistence.|
|HTTP||REST||Perform CRUD operations of your persistence model using HTTP and Spring Data Repositories.|
|Key Value Stores||Redis||Redis is an open source, advanced key-value store.|
|Key Values Stores||Riak||Riak is a Dynamo-inspired key/value store with a distributed database network platform that makes storing and retrieving data simple, safe and low-cost.|
|Document Stores||MongoDB||MongoDB is a scalable, high-performance, open source, document-oriented database.|
|Graph Databases||Neo4j||Neo4j is a graph database, a fully transactional database that stores data structured as graphs.|
|Column Stores||HBase||Apache HBase is an open-source, distributed, versioned, column-oriented store modeled after Google’ Bigtable|
|Column Stores||Cassandra||The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo’s fully distributed design and Bigtable’s ColumnFamily-based data model.|
|Blob-Stores||Blob||Provides access to blob stores such as Amazon Simple Storage Services (S3) as well as from other vendors such as Rackspace and Azure.|
|Common Infrastructure||Commons||Provides shared infrastructure for use across various data access projects. General support for cross-database persistence is located here|
|Common Infrastructure||Grails Mapping||Provides a database unified object mapping framework that is portable across different databases for Grails.|
You can get more details from the URL: http://www.springsource.org/spring-data
Again, covering Spring Data completely is out of scope of this book, but since these are very important for any modern day application, I have picked Spring Data JPA to explain the support provided by Spring Data to ease developing using Spring and data access technology JPA.
Spring Data JPA is part of the umbrella Spring Data project that makes it easy to implement JPA based repositories (Chapter 1 covered Repository pattern).
Implementing a data access layer of an application has been burdensome for quite a while. Developers have to write too much plumbing code to achieve executing simple queries as well performing auditing and pagination. Spring Data JPA tries to address this so that developers can spend more time writing actual business logic required for the application.
Using Spring JPA, you will only write repository interface with custom finder methods and Spring JPA will provide you with the actual implementation for these methods automatically. The approach seamlessly works with other ersistenct stores such as MongoDB, Neo4j as part of other projects like Spring MongoDB and Spring Neo4j respectively.
Page Visitors: 2370