Spring Book – Chapter 21 – Spring Batch

Hibernate

Spring Batch provides two Hibernate item readers classes namely:

  • org.springframework.batch.item.database.HibernateCursorItemReader – It’s an  for reading database records built on top of Hibernate.  It executes the HQL query when initialized and iterates over the result set, returning an object corresponding to current row.
  • org.springframework.batch.item.database.HibernatePagingItemReader – It’s an  for reading database records built on top of Hibernate and reading only up to a fixed number of items at a time. It executes an HQL query when initialized and is paged accordingly.

To configure the HibernateCursorItemReader, you need to have two dependencies: a HibernateSessionFactory and the HQL string to be executed as shown in Listing 23-14 below.

Listing 23-14. Configuring a sample HibernateCursorItemReader

To configure the HibernatePagingItemReader, you need to have three dependencies: a HibernateSessionFactory, page size and the HQL string to be executed as shown in Listing 23-15 below.

Listing 23-15. Configuring a sample HibernatePagingItemReader

HibernateItemWriter is an ItemWriter that uses a Hibernate session to save or update entities that are not part of the current Hibernate session. It will also flush the session at chunk boundaries. To configure the HibernateItemWriter, you need to have one dependency: a HibernateSessionFactory as shown in Listing 23-16 below.

Listing 23-16. Configuring a sample HibernateItemWriter

JPA

Spring Batch provides org.springframework.batch.item.database.JpaPagingItemReader class for supporting JPA. It’s an ItemReader for reading database records built on top of JPA. It executes the JPQL to retrieve requested data. The query is executed using paged requests of size specified in the Spring configuration. The performance of the paging depends on the JPA implementation and its use of database specific features to limit the number of returned rows.

To configure the JpaPagingItemReader, you need to have three dependencies: the entityManager, a query to execute, query parameter if you have one as shown in Listing 23-17 below.

Listing 23-17. Configuring a sample JpaPagingItemReader

JpaItemWriter is an ItemWriter that is using a JPA EntityManagerFactory to merge any Entities that aren’t part of the persistence context. It is required that write(List) is called inside a transaction.. To configure the JpaItemWriter, you need to have one dependency: a entityManager as shown in Listing 23-18 below.

Listing 23-18. Configuring a sample JpaItemWriter

Page Visitors: 25940

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.

    Thanks
    Laxmi

Leave a Reply

Your email address will not be published. Required fields are marked *