0

I have this POJO:

public class SomeEntity {
    private String recordName;
    private Instant recordDatetime;

    public SomeEntity(String recordName, Instant recordDateTime) {
        this.recordName = recordName;
        this.recordDateTime = recordDateTime;
    }

    // Getters and Setters
}

...and this corresponding database table defined in a SQL Server 2016 database:

CREATE TABLE [dbo].[some_entities](
    [record_name] [nvarchar](255) NOT NULL,
    [record_datetime] [datetimeoffset](7) NOT NULL,
 CONSTRAINT [PK_some_entities] PRIMARY KEY CLUSTERED 
(
    [record_name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
)
GO

I have a DAO implementation class bean defined in my Spring Boot application that inserts a record into this table and reads records off the table. I am having a couple of problems though.

  • The insert fails with the exception:

    com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206)
    at com.microsoft.sqlserver.jdbc.DataTypes.throwConversionError(DataTypes.java:1652)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObject(SQLServerPreparedStatement.java:1461)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObjectNoType(SQLServerPreparedStatement.java:1320)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObject(SQLServerPreparedStatement.java:1329)
    
  • Retrieving the records from the database via the getSomeEntities() method is throwing an exception too:

    nested exception is java.sql.SQLFeatureNotSupportedException: This operation is not supported.
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:94)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:711)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:761)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:204)
    

The DAO implementation class is as follows:

@Repository
public class SomeEntityDaoImpl implements SomeEntityDao {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public void insertNewRecord(SomeEntity someEntity) throws Exception {

        Map<String,Object> namedParameters = new HashMap<>();
        namedParameters.put("record_name", someEntity.getRecordName());
        namedParameters.put("record_datetime", someEntity.getRecordDatetime());
        try {

            int returnVal = jdbcTemplate.update("INSERT INTO some_entities (record_name, record_datetime) VALUES (:record_name, :record_datetime)", namedParameters);
            if (returnVal != 1) {
                logger.error("Could not insert into database: {} record(s) updated", returnVal);
                throw new Exception("insertion failed");
            }
        }
    }

    @Override
    public List<SomeEntity> getSomeEntities() throws Exception {

        List<SomeEntity> someEntities = new ArrayList<>();
        try {

            someEntities = jdbcTemplate.query("SELECT * FROM some_entities", (resultSet, rowNum) -> {

                SomeEntity someEntity = new SomeEntity(
                resultSet.getString("record_name"),
                resultSet.getObject("record_datetime", Instant.class));

                return someEntity;
            });
        } catch (DataAccessException dae) {
            logger.error("Failed to get some entities: {}", dae.getMessage(), dae);
            throw new Exception("Database exception");
        }

        return someEntities;
    }
}

Stack trace:

org.springframework.dao.InvalidDataAccessApiUsageException: PreparedStatementCallback; SQL [SELECT * FROM some_entities]; This operation is not supported.; nested exception is java.sql.SQLFeatureNotSupportedException: This operation is not supported
java.sql.SQLFeatureNotSupportedException: This operation is not supported.
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:94)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:711)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:761)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:211)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:219)

Spring Boot: 1.5.7.RELEASE
SQL Server: 2016
Maven dependency: 
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

I got the start from another comprehensive StackOverflow answer, but since it was not specific to Spring JDBC, I am not sure if I am following the suggested approach correctly.

Web User
  • 7,438
  • 14
  • 64
  • 92
  • First of all, using `OffsetDateTime` does not really add any value here. Use `Instant` for storing, exchanging, and keeping a moment in your class. Another thing, this example is rather complicated for a Stack Overflow posting. Try to simplify, removing all the parts not directly related to your issue. In doing so, you may well solve your problem. Lastly, explicitly call out your specific database, driver, and the data type of your date-time column. – Basil Bourque Jun 05 '18 at 19:09
  • @BasilBourque I already attempted to simplify while also wanting to illustrate the full flow starting from the DB schema and corresponding POJO, in addition to using Spring JDBC to perform the insert and retrieve operations, since I am running into anomalous results in both scenarios. Point taken on the use of `Instant` though... I modified the code as well as the question to use this class. – Web User Jun 05 '18 at 19:18

0 Answers0