Skip to content

Data class support in JdbcCursorItemReaderBuilder & JdbcPagingItemReaderBuilder #4578

Closed
@beccagaspard

Description

@beccagaspard

Expected Behavior

Add support to be able to easily configure a DataClassRowMapper when using JdbcCursorItemReaderBuilder & JdbcPagingItemReaderBuilder - similar to the existing beanRowMapper functionality added in #895 and #819. This would make it easier to set up jdbc item readers when the target class is a Java record or Kotlin data class.

One approach could be to modify the existing JdbcCursorItemReaderBuilder::beanRowMapper & JdbcPagingItemReaderBuilder::beanRowMapper methods to set a DataClassRowMapper instead of a BeanPropertyRowMapper - according to the docs it is backwards compatible.

Note that this class extends BeanPropertyRowMapper and can therefore serve as a common choice for any mapped target class, flexibly adapting to constructor style versus setter methods in the mapped class.

public JdbcCursorItemReaderBuilder<T> beanRowMapper(Class<T> mappedClass) {
	this.rowMapper = new DataClassRowMapper<>(mappedClass);

	return this;
}

A second approach could be to add new builder methods JdbcCursorItemReaderBuilder::dataRowMapper & JdbcPagingItemReaderBuilder::dataRowMapper that create and set a DataClassRowMapper.

public JdbcCursorItemReaderBuilder<T> dataRowMapper(Class<T> mappedClass) {
	this.rowMapper = new DataClassRowMapper<>(mappedClass);

	return this;
}

If there is a preferred approach, please let me know and I will be happy to try to contribute a PR for the changes! 🤓

Current Behavior

JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReaderBuilder<Foo>()
        ...
	.beanRowMapper(Foo.class)
        .build();

and

JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>()
        ...
	.beanRowMapper(Foo.class)
        .build();

will create item readers w/ a BeanPropertyRowMapper. From the docs:

If you need to map to a target class which has a data class constructor — for example, a Java record or a Kotlin data class — use DataClassRowMapper instead.

Context
As we convert batch jobs to use java record classes, we find ourselves needing to change
.beanRowMapper(Foo.class) -> .rowMapper(new DataClassRowMapper<>(FooRecord.class)) over and over again.

Might be similar to #4160, #4568

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions