Skip to content

JdbcTemplate should support JDBC 4.1 getObject(columnIndex, type) [SPR-11600] #16223

Closed

Description

@spring-projects-issues

Philippe Marschall opened SPR-11600 and commented

JDBC 4.2 defines the following type mappings

ANSI SQL Java SE 8
DATE LocalDate
TIME LocalTime
TIMESTAMP LocalDateTime
TIME WITH TIMEZONE OffsetTime
TIMESTAMP WITH TIMEZONE OffsetDateTime

with can be used through ResultSet#getObject(int, Class), ResultSet#getObject(String, Class),PreparedStatement#setObject(int, Object)

So I would expect the following to work

jdbcTemplate.queryForObject("SELECT DATE_COLUMN FROM JAVA_TIME", LocalDate.class);

but it doesn't, it fails with:

org.springframework.dao.TypeMismatchDataAccessException: Type mismatch affecting row number 0 and column type 'DATE': Value [1988-12-25] is of type [java.sql.Date] and cannot be converted to required type [java.time.LocalDate]

Instead I have to do

jdbcTemplate.queryForObject("SELECT DATE_COLUMN FROM JAVA_TIME",
        (rs, rowNum) -> rs.getObject(1, LocalDate.class));

The issue seems to be that JdbcUtils.getResultSetValue(ResultSet, int, Class<?>) calls JdbcUtils.getResultSetValue(ResultSet, int) without the required type. I don't know if this an oversight or intentional.


Affects: 4.0.2

Issue Links:

Referenced from: commits 3fef358, 3767938, c9432ce

Activity

  1. spring-projects-issues commented on Mar 25, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    That JdbcUtils code doesn't take JDK 7's JDBC 4.1 into account yet, which is where getObject(columnIndex, type) showed up for the first time. We'll make sure to revise that code properly, using the new getObject variant when working with a JDBC 4.1+ driver (which unfortunately isn't trivial since common drivers are known to incompletely support the latest JDBC editions).

    Juergen

  2. spring-projects-issues commented on Apr 10, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    JdbcUtils uses JDBC 4.1 getObject(int, Class) for unknown ResultSet value types on JDK 7+ now, just falling back to a regular getObject(int) call as a last resort.

    I've also applied a general JDBC 3.0+ baseline upgrade, removing defensive measures such as catch blocks with pre-JDBC 3.0 assumptions.

    Juergen

  3. spring-projects-issues commented on Apr 11, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Philippe Marschall commented

    Thank you

  4. spring-projects-issues commented on Aug 24, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Pavel Alexeev commented

    Does it already supported?

    In spring-jdbc 4.3.2.RELEASE i still got

    org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM lu_production_calendar WHERE  specified_date >= ? AND  specified_date <= ? ORDER BY specified_date ASC LIMIT ?]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.time.OffsetDateTime. Use setObject() with an explicit Types value to specify the type to use.
    	at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:711) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:761) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192) ~[spring-jdbc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    	at ru.rlh.egais.portal.backend.jdbc.EgaisNamedParameterJdbcTemplateImpl.query(EgaisNamedParameterJdbcTemplateImpl.java:74) ~[core-@[email protected]:na]
    	at ru.rlh.egais.portal.backend.dao.impl.lookup.ProductionCalendarDaoImpl.search(ProductionCalendarDaoImpl.java:54) ~[core-@[email protected]:na]
    	at ru.rlh.egais.portal.backend.service.lookup.ProductionCalendarService.search(ProductionCalendarService.java:26) ~[core-@[email protected]:na]
    

    According to source: https://github.com/spring-projects/spring-framework/blob/master/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java#L358 it known how to handle java.sql.Types.TIMESTAMP (java.util.Date, java.sql.Timestamp, Calendar) but not Types.TIMESTAMP_WITH_TIMEZONE (OffsetDateTime)?

  5. spring-projects-issues commented on Aug 25, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Philippe Marschall commented

    Yes it does, but your driver might not (yet). For Postgres you need version 9.4.1208 or later. You may have to set spring.jdbc.getParameterType.ignore to true in spring.properties.

  6. added
    in: data
    type: enhancement
    on Jan 11, 2019
  7. PhilippeHaution commented on Apr 3, 2024

    @PhilippeHaution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions