Skip to content
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

Data class support in JdbcCursorItemReaderBuilder & JdbcPagingItemReaderBuilder #4578

Open
beccagaspard opened this issue Apr 15, 2024 · 1 comment

Comments

@beccagaspard
Copy link

beccagaspard commented Apr 15, 2024

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

@beccagaspard beccagaspard added status: waiting-for-triage Issues that we did not analyse yet type: feature labels Apr 15, 2024
@fmbenhassine
Copy link
Contributor

Thank you for your suggestion, that's a good idea! Since Java beans and records are different, I think it makes more sense to have different methods in the builders.

You are welcome to contribute a PR!

@fmbenhassine fmbenhassine added in: infrastructure and removed status: waiting-for-triage Issues that we did not analyse yet labels Apr 25, 2024
@fmbenhassine fmbenhassine added this to the 5.2.0 milestone Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants