Java Reference
In-Depth Information
2.
Configure
EntityManagerFactory
and the JPA Transaction Manager.
EntityManagerFactory
is the source of an
EntityManager
for the job. It along
with a JPA-supported transaction manager is required.
3.
Map the
Customer
class. You use annotations to configure the mapping of the
Customer
class to the Customer table.
4.
Configure the
JpaItemWriter
. The last step is to configure the new ItemWriter
to save the items read in the job.
Let's start with the
persistence.xml
file, which is shown in Listing 9-36. This file needs to live in the
<PROJECT_HOME>/src/main/resources/META-INF/
directory with the name
persistence.xml
per the JPA
specification.
Listing 9-36.
persistence.xml
<persistence xmlns="
http://java.sun.com/xml/ns/persistence"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
version="1.0">
<persistence-unit name="customer" transaction-type="RESOURCE_LOCAL">
<class>com.apress.springbatch.chapter9.Customer</class>
</persistence-unit>
</persistence>
The
persistence.xml
file required for this example is about as simple as you can create. The
persistence unit is named
customer
with the application controlling the transactions. You have a single
class mapped with annotations, the
Customer
class. To get started with JPA, that is really all you need for
a
persistence.xml
file.
Next you can update the
launch-context.xml
file with an
EntityManagerFactory
and Spring's
JpaTransactionManager
. Listing 9-37 shows the additions you need to make to a base
launch-context.xml
file to incorporate the JPA components.
Listing 9-37.
Launch-context.xml
Updates for JPA
…
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="customer" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>