Java Reference
In-Depth Information
private String address;
private String city;
private String state;
private String zip;
// Accessors go here
...
}
The code in Listing 9-38 is the same that is in Listing 9-32. By avoiding the Hibernate annotations in
the previous example, you're able to see how switching persistence frameworks requires no code
changes.
The final aspect of configuring the job to use JPA is to configure JpaItemWriter . It requires only a
single dependency—a reference to EntityManagerFactory —so that it can obtain an EntityManager to
work with. Listing 9-39 shows the configuration for the new ItemWriter and the job updated to use it.
Listing 9-39. formatJob Configured to Use JpaItemWriter
...
<beans:bean id="jpaBatchWriter"
class="org.springframework.batch.item.database.JpaItemWriter">
<beans:property name="entityManagerFactory" ref="entityManagerFactory"/>
</beans:bean>
<step id="formatFileStep">
<tasklet>
<chunk reader="customerFileReader" writer="jpaBatchWriter"
commit-interval="10"/>
</tasklet>
</step>
<job id="formatJob">
<step id="step1" parent="formatFileStep"/>
</job>
...
You can now build the job with a quick mvn clean install . To execute the job, use the command in
Listing 9-40, which returns the results you've seen in the other database examples.
Listing 9-40. Command to Execute formatJob with JPA Configured
java -jar itemWriters-0.0.1-SNAPSHOT.jar jobs/formatJob.xml formatJob
customerFile=/input/customer.csv
The relational database rules in the modern enterprise, for better or worse. As you can see, writing
job results to a database is easy with Spring Batch. But files and databases aren't the only forms of
output that are available both from Spring Batch or needed in an enterprise. The next section looks at
other examples of the wide range of output options Spring Batch provides.
 
Search WWH ::




Custom Search