Cause java.sql.SQLDataException Unsupported conversion from LONG to java.sql.Timestamp

今天遇到了一个奇怪的错误,报错如下图所示:

org.springframework.dao.DataIntegrityViolationException: Error attempting to get column 'question_id' from result set.  Cause: java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestamp
; Unsupported conversion from LONG to java.sql.Timestamp; nested exception is java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestamp

	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:84)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy140.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:177)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:78)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy153.selectList(Unknown Source)
	at cn.tedu.straw.portal.test.mapper.TestTeacherQuestionMapper.selectOne(TestTeacherQuestionMapper.java:32)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestamp
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:114)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:96)
	at com.mysql.cj.jdbc.result.ResultSetImpl.getTimestamp(ResultSetImpl.java:903)
	at com.mysql.cj.jdbc.result.ResultSetImpl.getTimestamp(ResultSetImpl.java:948)
	at com.zaxxer.hikari.pool.HikariProxyResultSet.getTimestamp(HikariProxyResultSet.java)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:69)
	at com.sun.proxy.$Proxy248.getTimestamp(Unknown Source)
	at org.apache.ibatis.type.DateTypeHandler.getNullableResult(DateTypeHandler.java:39)
	at org.apache.ibatis.type.DateTypeHandler.getNullableResult(DateTypeHandler.java:28)
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:81)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createUsingConstructor(DefaultResultSetHandler.java:671)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:654)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:618)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:591)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:397)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301)
	at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy236.query(Unknown Source)
	at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doQuery(MybatisSimpleExecutor.java:67)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
	at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:155)
	at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:143)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
	at com.sun.proxy.$Proxy235.query(Unknown Source)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
	... 37 more

实体类:

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("teacher_question")
@ApiModel(value="TeacherQuestion对象", description="")
public class TeacherQuestion implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("teacher_id")
    private Integer teacherId;

    @TableField("question_id")
    private Integer questionId;

    @TableField("createtime")
    private Date createtime;



    public TeacherQuestion(Integer teacherId, Integer questionId, Date createtime) {
        this.teacherId = teacherId;
        this.questionId = questionId;
        this.createtime = createtime;
    }
}

mapper类和mapper文件如下:

public interface TeacherQuestionMapper extends BaseMapper<TeacherQuestion> {

}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.straw.portal.mapper.TeacherQuestionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.tedu.straw.portal.model.TeacherQuestion">
        <id column="id" property="id" />
        <result column="teacher_id" property="teacherId" />
        <result column="question_id" property="questionId" />
        <result column="createtime" property="createtime" />
    </resultMap>

</mapper>

数据库表如下:

测试类如下:

@SpringBootTest(classes = StrawPortalApplication.class)
@RunWith(SpringRunner.class)
public class TestTeacherQuestionMapper {
    @Resource
    private TeacherQuestionMapper teacherQuestionMapper;

    @Test
    public void selectOne(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("teacher_id",4);
        queryWrapper.eq("question_id",44);
        List<TeacherQuestion> teacherQuestions = teacherQuestionMapper.selectList(queryWrapper);
        System.out.println(teacherQuestions);
    }

}

反复测试了好多次,不管什么测试条件都是报Cause: java.sql.SQLDataException: Unsupported conversion from LONG to java.sql.Timestamp错误。

后来才想起来自己在实体类里加了一个带参数的构造方法:

 public TeacherQuestion(Integer teacherId, Integer questionId, Date createtime) {
        this.teacherId = teacherId;
        this.questionId = questionId;
        this.createtime = createtime;
    }

这样就导致该实体类没有无参构造方法了,众所周知,一旦我们手动添加了带参数的构造方法,编译器就不会给我们添加默认的无参构造方法。导致mybatis无法new这个实体类。也就无法做映射。

解决的办法是:给该实体类手动添加一个无参构造方法。

 public TeacherQuestion() {

    }
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 这个错误信息是指在 Java 中试图将一个 long 类型的值转换为 java.sql.Timestamp 类型时出现了问题,因为这两种类型之间的转换是不支持的。 通常,java.sql.Timestamp 类型表示时间戳,其值是一个包含日期和时间信息的长整型数值,表示自 1970 年 1 月 1 日以来经过的毫秒数。如果要将一个 long 类型的值转换为 java.sql.Timestamp 类型,可以使用 java.sql.Timestamp 的构造函数来创建一个新的 java.sql.Timestamp 对象,例如: ``` long milliseconds = System.currentTimeMillis(); java.sql.Timestamp timestamp = new java.sql.Timestamp(milliseconds); ``` 这将创建一个新的 java.sql.Timestamp 对象,其值为当前时间戳。但是如果直接将 long 类型的值赋值给 java.sql.Timestamp 类型的变量,就会出现上述错误信息。 ### 回答2: unsupported conversion from long to java.sql.TimestampJava编程中,时间戳是从2019年1月1日开始计算的毫秒数,可以使用long类型来表示。而java.sql.TimestampJava中表示时间戳的一个类,通常用于表示数据库中的日期和时间数据。 当我们在程序中需要将long类型的毫秒数转换为java.sql.Timestamp类型时,我们通常会使用如下代码: long time = 1575460274707L; //时间戳的毫秒表示 java.sql.Timestamp timestamp = new java.sql.Timestamp(time); //将long类型转换为Timestamp类型 但是,有时我们会遇到unsupported conversion from long to java.sql.Timestamp的异常,这是因为long类型和java.sql.Timestamp类型之间并没有直接的转换方式。 那么,如何解决这个异常呢? 一种常见的解决方案是使用java.util.Date类来转换时间戳。我们可以先将long类型的毫秒数转换为Date类型,然后再使用java.sql.Timestamp类将其转换为数据库中的时间格式。例如: long time = 1575460274707L; //时间戳的毫秒表示 java.util.Date date = new java.util.Date(time); //将long类型转换为Date类型 java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime()); //将Date类型转换为Timestamp类型 另外,如果我们使用的是JDBC 4.2及以上的版本,也可以使用新的方法toInstant()将java.sql.Timestamp类型转换为Java 8的时间类型Instant,然后再通过toEpochMilli()方法获取毫秒数。例如: java.sql.Timestamp timestamp = new java.sql.Timestamp(time); //获取一个java.sql.Timestamp对象 Instant instant = timestamp.toInstant(); //将Timestamp对象转换为Instant类型 long timeInMillis = instant.toEpochMilli(); //通过Instant对象获取毫秒数 总之,当我们在Java编程中遇到unsupported conversion from long to java.sql.Timestamp的异常时,应该选择合适的方式将long类型的毫秒数转换为java.sql.Timestamp类型,以满足我们对时间戳的处理需求。 ### 回答3: 长整型数和时间戳是两种不同的数据类型,它们之间不能进行直接转换。在Java语言中,时间戳是使用java.sql.Timestamp类来表示的,而长整型数通常使用Java中的long类型表示。由于它们的数据类型不同,直接进行转换会导致Java编译器抛出“unsupported conversion from long to java.sql.timestamp”的错误。 解决这个问题的方法主要有两种:一种是使用java.util.Date类来代替long类型进行时间戳的转换,另一种是使用Java提供的日期格式化类进行时间戳和字符串之间的转换。下面分别介绍这两种方法的具体实现步骤。 1. 使用java.util.Date类转换 使用java.util.Date类进行时间戳转换的主要思路是利用Date类提供的构造函数将long类型的时间戳转换为Date对象,然后再将Date对象转换为java.sql.Timestamp类型。具体实现代码如下: ``` long time = System.currentTimeMillis(); // 获取当前时间戳 Date date = new Date(time); // 将长整型时间戳转换为日期对象 Timestamp timestamp = new Timestamp(date.getTime()); // 将日期对象转换为时间戳对象 ``` 2. 使用日期格式化类转换 Java提供了很多日期格式化类来处理时间和日期的转换。其中,最常用的类是SimpleDateFormat类。这个类可以将时间戳转换为需要的日期格式的字符串,也可以将日期格式的字符串转换为时间戳。具体实现代码如下: ``` long time = System.currentTimeMillis(); // 获取当前时间戳 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式 String strTime = sdf.format(new Date(time)); // 将时间戳转换为字符串 Timestamp timestamp = Timestamp.valueOf(strTime); // 将字符串转换为时间戳对象 ``` 总之,无论使用哪种方法,都要注意数据类型的匹配,以便正确地进行时间戳的转换。如果数据类型不匹配,就会出现编译错误,导致不能正确地进行转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值