2020-06-28

Spring Batch JpaPagingItemReader Example

JpaPagingItemReader

Spring Batch 4.3.0 Add support for named queries in JpaPagingItemReader.

Up until now, it was possible to use named queries with the JpaPagingItemReader. However, this required the creation of a custom query provider, as follows:

JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>()
    .name("fooReader")
    .queryProvider(new AbstractJpaQueryProvider() {
       @Override
       public Query createQuery() {
          return getEntityManager().createNamedQuery("allFoos", Foo.class);
       }

       @Override
       public void afterPropertiesSet() throws Exception {
       }
    })
    // set other properties on the reader
    .build();

In this release, we introduced a JpaNamedQueryProvider next to the JpaNativeQueryProvider
to ease the configuration, which can now be written like this:

JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>()
.name("fooReader")
.queryProvider(new JpaNamedQueryProvider("allFoos", Foo.class))
// set other properties on the reader
.build();

No comments:

Post a Comment