Java Reference
In-Depth Information
Over the past few years, the majority of innovation in the Java world has been in the open source space.
Spring, Hibernate, the various JVM languages like Clojure, Scala, and JRuby that have come onto the
scene. All of these have been products of the open source movement. As those products have shown
their usefulness, Sun (now Oracle) has integrated similar approaches into the native Java API. JPA is one
such example. In this case, Gavin King (the original author of Hibernate) was one of the driving forces in
the development of JSR 220. In this section, you will look at how to use the JPA as your method of
database access for your batch processes.
Although, as you will see, JPA has a number of similarities to Hibernate, it is not a drop-in
replacement. One of the notable features missing from the JPA capabilities that Hibernate supports is
the ability to use a cursor for database access. JPA supports paging but not cursor driven access. In this
example, you will use JPA to provide paged database access similar to the Hibernate paged example you
used previously.
To configure JPA, you will need to create the JPA version of the hibernate.cfg.xml , the
persistence.xml as well as update launch-context.xml and copyJob.xml . It is important to note that
since you used the JPA annotations in your Hibernate example, there will be no need to change your
Customer object itself. Let's get started by creating the persistence.xml file.
The persistence.xml file must reside in the META-INF folder of your built jar file per the Java spec. To
get it there, you will create your file in the <project_root>/src/main/resources/META-INF directory.
Maven will take care of getting this directory in the correct spot of your jar file when you build it later.
The contents of your persistence.xml file are about as simple as you can get. The only thing you need it
for is to define your persistence unit (a customer object) and associate it with the correct class (your
Customer class). Listing 7-49 shows the persistence.xml file you will use.
Listing 7-49. persistence.xml
<persistence xmlns=""
<persistence-unit name="customer" transaction-type="RESOURCE_LOCAL">
With the persistence.xml file configured, you can update your launch-context.xml to use Spring's
org.springframework.orm.jpa.JpaTransactionManager and its EntityManager, and to update the
jobRepository. The configuration for your JpaTransactionManager and EntityManager should look very
familiar. They are almost identical to the Hibernate configurations for the
HibernateTransactionManager and SessionFactory, respectively. The small tweak you need to make to
your jobRepository implementation is that you need to define the transaction isolation level to create
ISOLATION_DEFAULT. Listing 7-50 shows launch-context.xml configured to use JPA for persistence.
Listing 7-50. launch-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
Search WWH ::

Custom Search