Hibernate
Spring Batch provides two Hibernate item readers classes namely:
- org.springframework.batch.item.database.HibernateCursorItemReader – It’s an
1ItemReader
- org.springframework.batch.item.database.HibernatePagingItemReader – It’s an
1ItemReader
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
… <bean id="sampleItemReader"class="org.springframework.batch.item.database.HibernateCursorItemReader" scope="step"> <property name="sessionFactory" ref="sessionFactory"/> <property name="queryString" value="…"/> <property name="parameterValues"> … </property> </bean> … |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
… <bean id="sampleItemReader" class="org.springframework.batch.item.database.HibernatePagingItemReader" scope="step"> <property name="sessionFactory" ref="sessionFactory"/> <property name="queryString" value="…"/> <property name="parameterValues"> … </property> <property name="pageSize" value="10"/> </bean> … |
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
1 2 3 4 5 6 7 8 9 |
… <bean id="sampleWriter" class="org.springframework.batch.item.database.HibernateItemWriter"> <property name="sessionFactory" ref="sessionFactory"/> </bean> … |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
… <bean id="sampleItemReader" class="org.springframework.batch.item.database.JpaPagingItemReader" scope="step"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="queryString" value="…" /> <property name="parameterValues"> … </property> </bean> … |
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
1 2 3 4 5 6 7 8 9 |
... <bean id="jpaSampleWriter" class="org.springframework.batch.item.database.JpaItemWriter"> <beans:property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> … |
Page Visitors: 25940
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?
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