Spring Book – Chapter 8 – Data Access

Spring and JPA

Yes, the chapter is really long. We have explained in detail on the integration of Spring with Hibernate and iBatis. I cannot ignore Java Persistence API (JPA) in this chapter which details data access methodologies which Spring supports to be used in your application with ease. This section details the integration of Spring with JPA.

JPA is designed to operate on domain objects and is defined as POJO’s with special annotations which are used as metadata to do the real mapping between the POJO and the actual database tables. It replaces previous persistence mechanisms like EJB entity beans and JDO (Java Data Objects). It provides common API for object-relational mapping and is direct learning from existing products like Hibernate, TopLink, etc. There are some very key concepts that you would like to know before diving deep into Spring and JPA integration. They are:

  • EntityManager – this is the resource manager that maintains the active collection of entity objects in your application. It handles the database interaction and meta-data for object-relational mappings. An instance of this actually represents a persistence context. An application can obtain it by injection into the application or by looking it up in the Java component name-space. If the application manages its persistence, the EntityManager is obtained from the EntityManagerFactory.
  • EntityManagerFactory – used to create an EntityManager for database interactions. The application server containers typically supply this function, but the EntityManagerFactory is required if you are using JPA application-managed persistence.
  • Persistence Unit – consists of the declarative meta-data that describes the relationship of entity class objects to a relational database. The EntityManagerFactory uses this data to create a persistence context that can be accessed through the EntityManager.

The listings in this section show the actual Spring and JPA integration in action.  Some steps are identical to Hibernate and iBatis, but I am repeating it as is so that the reader doesn’t have to turn pages and loose the flow of reading. The various steps involved are:

1. Database table creation – create the table according to the database that you are using. Since we have taken Customer entity for explaining this integration example, create the customer table with all the relevant columns. For ease of understanding, I will be using only three fields in the Customer entity namely ‘First Name’, ‘Last Name’ and the primary key ‘ID’. Step same as Hibernate.

2. POJO (Customer) – The Customer POJO containing JPA annotations which provides metadata for linking the POJO and the corresponding database table. The class name is used as table name by default.

Listing 8-53. Customer POJO containing JPA related annotations

3. CustomerRepository interface – the various methods in the interface which the implementation class namely JpaCustomerRepository will implement using JPA.

Listing 8-54. CustomerRepository interface containing relevant methods

4. persistence.xml – usually, JPA defines a persistence unit through the META-INF/persistence.xml file. Starting with Spring 3.1, this XML file is no longer necessary – the LocalContainerEntityManagerFactoryBean now supports a ‘packagesToScan’ property where the packages to scan for @Entity classes can be specified. If at all you want to do the old way of doing things in JPA, create a new file namely persistence.xml inside the META-INF folder with the following contents as shown in Listing 8-1 below. You will then have to set the property ‘persistenceUnitName’ while configuring your entity manager.

Listing 8-55. Sample persistence.xml file if you are using Spring earlier than 3.1

5. CustomerRepository/CustomerDao implementation class – the Listing 8-56 shows the actual implementation of the CustomerRepository interface using JPA.

Listing 8-56. CustomerRepository implementation class for JPA

6. Spring configuration

a. DataSource configuration – datasource configuration in Spring configuration file. Uses MySql in Listing 8-57.

Listing 8-57. DataSource configuration in Spring configuration

b. EntityManager bean configuration – the main bean which is key to do persistence using JPA. The property namely ‘jpaVendorAdapter’ in Listing 8-58 uses Hibernate JPA, which is one of the implementations of JPA specification. There are other implementations of this, namely:

    1. EclipseLink
    2. Apache OpenJPA
    3. Data Nucleus
    4. Hibernate EntityManager

Listing 8-58. EntityManager bean configuration

c. CustomerRepository/CustomerDao configuration – the actual implementation class of CustomerRepository interface is configured.

Listing 8-59. CustomerRepository bean for JPA implementation

7. Testing class – testing class which shows the actual working of Spring and JPA. Usually the consumers of these repositories will the service classes.

Listing 8-60. Testing class for testing Spring and JPA integration

Summary

In this chapter I have explained in detail on the various data access technology support which the Spring framework provides you to utilize it effectively in your application. Using it enables you to have layered architecture principles in your application. If your design is good enough, the higher layer will never know the data management technologies used beneath and using Spring IoC, it can be swapped very easily down the layer according to your application requirement.

Spring also gives very good way of isolating various exceptions that the various data access technology throws into some meaningful exceptions using the DataAccessException hierarchy in a consistent and elegant way.

I the later sections of the chapter I consciously took time to explain in some detail the various important technologies prominent in the present application development scenario.

Page Visitors: 9022

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 8 – Data Access

Leave a Reply

Your email address will not be published. Required fields are marked *