Skip to content

Add support for query hints in JPA item readers #4479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maciejwalkowiak opened this issue Nov 2, 2023 · 2 comments
Closed

Add support for query hints in JPA item readers #4479

maciejwalkowiak opened this issue Nov 2, 2023 · 2 comments

Comments

@maciejwalkowiak
Copy link

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.

@maciejwalkowiak maciejwalkowiak added status: waiting-for-triage Issues that we did not analyse yet type: feature labels Nov 2, 2023
@fmbenhassine
Copy link
Contributor

Thank you for raising this, @maciejwalkowiak ! Support for query hints is indeed missing in JpaCursorItemReader and JpaPagingItemReader as well. Query hints should be set on the query just like query parameters. Respective builders should be updated as well.

I will plan this for v5.2 (the upcoming v5.1 is in feature freeze at this point). Contributions are welcome!

@fmbenhassine fmbenhassine added in: infrastructure and removed status: waiting-for-triage Issues that we did not analyse yet labels Nov 6, 2023
@fmbenhassine fmbenhassine added this to the 5.2.0 milestone Nov 6, 2023
@baezzys
Copy link
Contributor

baezzys commented Nov 12, 2023

Hi @fmbenhassine, I want to contribute to this issue. I'll create PR soon.

@fmbenhassine fmbenhassine changed the title Add fetchSize to JpaCursorItemReaderBuilder Add support for query hints in JPA item readers Apr 2, 2024
@fmbenhassine fmbenhassine modified the milestones: 5.2.0, 5.2.0-M1 Apr 8, 2024
FBibonne pushed a commit to FBibonne/spring-batch that referenced this issue Feb 2, 2025
Enhanced `JpaCursorItemReader`, `JpaCursorItemReaderBuilder`,
`JpaPagingItemReader`, and `JpaPagingItemReaderBuilder` with
query hints configuration. The inclusion of query hints in both
cursor and paging item readers improves query execution strategies,
optimizing performance for complex data retrieval scenarios.

Resolves spring-projects#4479

Signed-off-by: Fabrice Bibonne <fabrice.bibonne@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants