I have looked one by one at all of the other questions, and I am not overcoming my issue. I have vastly simplified the code, and I have what I believe is a novel demonstration of the issue.
This code works:
@Repository
public class MyFriend {
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public List<TransmitUnit> getTransmissionList(LocalDate workingDate)
{
List<TransmitUnit> retValue = null;
String sql = "SELECT working_day as workingDate, origin_code as originCode FROM core.sort_transmission WHERE working_day = '2024-08-23'";
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("working_day", workingDate.toString());
retValue = namedParameterJdbcTemplate.query(sql, namedParameters, new BeanPropertyRowMapper<>(TransmitUnit.class));
for (TransmitUnit u: retValue) {
LOGGER.info("Got this tu {}", u.getWorkingDate());
}
return retValue;
}
If I replace the '2024-08-23' in sql
with a named parameter:
String sql = "SELECT working_day as workingDate, origin_code as originCode
FROM core.sort_transmission WHERE working_day = :working_day";
Now, I get an error in the statement:
ERRO class="MyFriend" method="getTransmissionList" transactionId="6913d8d6-d56a-4c8f-bd78-32c976817d39-1724878713995"
request="[2024-08-28]" errorType="org.springframework.jdbc.BadSqlGrammarException" errorMsg="PreparedStatementCallback; bad SQL grammar
[SELECT working_day as workingDate, origin_code as originCode
FROM core.sort_transmission WHERE working_day = ?]" errorOrigin="org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:104)"
I am wondering why the SQL shows a ?
as if this was a generic prepared statement instead of with a named parameter. I know this has worked before, I am not sure what I could be doing wrong.
EDIT: Based on John Bollinger's comment I submitted an answer that I think nicely illustrates the exact type problem and would be enlightening for future viewers.
John Bollinger was correct. The type in Java needs to match the type in SQL. Removing the toString() worked.
Final working code:
@Repository
public class MyFriend {
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public List<TransmitUnit> getTransmissionList(LocalDate workingDate)
{
List<TransmitUnit> retValue = null;
String sql = "SELECT working_day as workingDate, origin_code as originCode FROM core.sort_transmission WHERE working_day = :working_day";
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("working_day", workingDate);
retValue = namedParameterJdbcTemplate.query(sql, namedParameters, new BeanPropertyRowMapper<>(TransmitUnit.class));
for (TransmitUnit u: retValue) {
LOGGER.info("Got this tu {}", u.getWorkingDate());
}
return retValue;
}