Java Reference
In-Depth Information
<beans:property name="prototypeBeanName" value="customer"/>
<beans:bean id="customer" class="com.apress.springbatch.chapter9.Customer"
<beans:bean id="hibernateBatchWriter"
<beans:property name="sessionFactory" ref="sessionFactory"/>
<step id="formatStep">
<chunk reader="customerFileReader" writer="hibernateBatchWriter"
<job id="formatJob">
<step id="step1" parent="formatStep"/>
The configuration for this job changes only with the configuration of hibernateBatchWriter and its
reference in the formatStep . As you saw previously, HibernateItemWriter requires only a reference to a
SessionFactory , which is provided via the configuration in launch-context.xml . Executing this job
returns the same results as the JdbcBatchItemWriter example previously.
When other frameworks do all of the heavy lifting, the Spring Batch configuration is quite simple, as
this Hibernate example shows. Hibernate's official spec cousin, JPA, is the other database access
framework you can use to do database writing.
The Java Persistence API (JPA) provides very similar functionality and requires almost the exact same
configuration as its Hibernate cousin. It, like Hibernate, does the heavy lifting in the case of writing to
the database, so the Spring Batch piece of the puzzle is very small. This section looks at how to configure
JPA to perform database writing.
When you look at the org.springframework.batch.item.writer.JpaItemWriter , it serves as a thin
wrapper around JPA's javax.persistence.EntityManager . When a chunk completes, the list of items
within the chunk is passed to JpaItemWriter . The writer loops over the items in the list, calling the
EntityManager 's merge method on each item before calling flush after all the items have been saved.
To see JpaItemWriter in action, you use the same customer input as earlier and insert it into the
same Customer table. To hook JPA into the job, you need to do the following four things:
Add a persistence.xml file. The persistence.xml file in JPA is used to configure
the EntityManager . For you to be able to use JPA, you need to add one to the
Search WWH ::

Custom Search