Skip to content

Introduce queryForOptional() method in JdbcTemplate #31644

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

Conversation

vuriaval
Copy link

This pull request introduces the queryForOptional method, aimed at simplifying SQL query result retrieval with a RowMapper. The goal is to eliminate boilerplate code often needed to handle empty results, offering a cleaner alternative to queryForObject.

Example of usage:

Optional<Integer> age = jdbcTemplate.queryForOptional("SELECT AGE FROM CUSTMR WHERE ID = ?", (rs, rowNum) -> rs.getInt(1), 3);

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 21, 2023
@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Nov 21, 2023
@sbrannen sbrannen changed the title Add queryForOptional method in JdbcTemplate Introduce queryForOptional() method in JdbcTemplate Nov 21, 2023
@sbrannen sbrannen self-assigned this Nov 21, 2023
@sbrannen
Copy link
Member

Hi @vuriaval,

Congratulations on submitting your first PR on GitHub! 👍

Unfortunately, we do not intend to introduce additional overloaded methods in JdbcOperations/JdbcTemplate to support Optional in that fashion.

Instead, please use the fluent API available in the new JdbcClient:

Optional<Integer> value = jdbcClient.sql("SELECT AGE FROM CUSTMR WHERE ID = ?")
		.param(1, 3)
		.query((rs, rowNum) -> rs.getInt(1))
		.optional();

In light of that, I am closing this PR.

@sbrannen sbrannen closed this Nov 21, 2023
@snicoll
Copy link
Member

snicoll commented Nov 21, 2023

See also #724 and the issues that it links to where this has been discussed several times.

@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants