Java Reference
In-Depth Information
<beans:property name="prototypeBeanName" value="customer"/>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="customer" class="com.apress.springbatch.chapter9.Customer"
scope="prototype"/>
<beans:bean id="hibernateBatchWriter"
class="org.springframework.batch.item.database.HibernateItemWriter">
<beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
<step id="formatStep">
<tasklet>
<chunk reader="customerFileReader" writer="hibernateBatchWriter"
commit-interval="10"/>
</tasklet>
</step>
<job id="formatJob">
<step id="step1" parent="formatStep"/>
</job>
</beans:beans>
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.
JpaItemWriter
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:
1.
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
project.
 
Search WWH ::




Custom Search