Expected Behavior
JpaCursorItemReaderBuilder should have an option to set fetchSize, similar to HibernateCursorItemReader (which is deprecated) and JdbcCursorItemReaderBuilder.
Current Behavior
There is no option to fetch size for query executed by JpaCursorItemReader. As a workaround I created FetchSizeAwareJpaQueryProvider. Pasting here in case someone would like to use it:
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import org.hibernate.jpa.AvailableHints;
import org.springframework.batch.item.database.orm.JpaQueryProvider;
/**
* {@link JpaQueryProvider} implementation that enables setting "FETCH_SIZE" query hint on the JPA query.
*
* <a href="https://jdbc.postgresql.org/documentation/query/#getting-results-based-on-a-cursor">Getting results based on a cursor</a>
*
* @author Maciej Walkowiak
*/
public class FetchSizeAwareJpaQueryProvider implements JpaQueryProvider {
private final String queryString;
private final int fetchSize;
private EntityManager entityManager;
public FetchSizeAwareJpaQueryProvider(String queryString, int fetchSize) {
this.fetchSize = fetchSize;
this.queryString = queryString;
}
@Override
public Query createQuery() {
Query query = this.entityManager.createQuery(queryString);
query.setHint(AvailableHints.HINT_FETCH_SIZE, fetchSize);
return query;
}
@Override
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
.. and use it like this:
new JpaCursorItemReaderBuilder<...>()
.name("...")
.entityManagerFactory(..)
.queryProvider(new FetchSizeAwareJpaQueryProvider("<jpql query>", 100))
.build();
If it makes sense I can see if I find time to contribute it.
Expected Behavior
JpaCursorItemReaderBuildershould have an option to setfetchSize, similar toHibernateCursorItemReader(which is deprecated) andJdbcCursorItemReaderBuilder.Current Behavior
There is no option to fetch size for query executed by
JpaCursorItemReader. As a workaround I createdFetchSizeAwareJpaQueryProvider. Pasting here in case someone would like to use it:.. and use it like this:
If it makes sense I can see if I find time to contribute it.