问题
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.
来源:https://stackoverflow.com/questions/50693048/unable-to-insert-datetimeoffset-value-using-spring-boot-on-sql-server-2016