Spring 异常:java.sql.SQLDataException:不支持从时间戳到java.lang.Long的转换

Spring 异常:java.sql.SQLDataException:不支持从时间戳到java.lang.Long的转换,spring,java-8,jpa-2.1,mysql-8.0,Spring,Java 8,Jpa 2.1,Mysql 8.0,我正在开发一个功能,需要根据日期从数据库中获取数据。DB中的日期格式为yyyy-MM-dd,在我的应用程序中,我以相同的形式传递日期 Detritable实体类中的day列是Date(java.util.Date)。我也使用了临时的,但没有成功 下面是java代码: EntityManagerFactory emf= Persistence.createEntityManagerFactory("punit"); EntityManager em = emf.cr

我正在开发一个功能,需要根据日期从数据库中获取数据。DB中的日期格式为yyyy-MM-dd,在我的应用程序中,我以相同的形式传递日期

Detritable实体类中的day列是Date(java.util.Date)。我也使用了临时的,但没有成功

下面是java代码:

        EntityManagerFactory emf= Persistence.createEntityManagerFactory("punit");
        EntityManager em = emf.createEntityManager();

        Query query;
        query = em.createQuery("select r from TempTable r where r.uId = :uid and r.day = :day group by r.mId, r.rId");
        query.setParameter("uid", 5l);

        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd");
        Date d = simpleDateFormat.parse("2018-10-12");

        query.setParameter("day", d);

        query.getResultList();
执行代码后,发现如下异常:

Jan 13, 2020 3:11:20 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unsupported conversion from TIMESTAMP to java.lang.Long
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1535)
    at org.hibernate.query.Query.getResultList(Query.java:165)
    at com.abc.XXXXX.main(XXXXX.java:30)
Caused by: org.hibernate.exception.DataException: could not execute query
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.loader.Loader.doList(Loader.java:2818)
    at org.hibernate.loader.Loader.doList(Loader.java:2797)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2629)
    at org.hibernate.loader.Loader.list(Loader.java:2624)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1396)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1558)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1526)
    ... 2 more
Caused by: java.sql.SQLDataException: Unsupported conversion from TIMESTAMP to java.lang.Long
    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.getObject(ResultSetImpl.java:1382)
    at com.mysql.cj.jdbc.result.ResultSetImpl.getLong(ResultSetImpl.java:812)
    at com.mysql.cj.jdbc.result.ResultSetImpl.getLong(ResultSetImpl.java:818)
    at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3041)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1866)
    at org.hibernate.loader.Loader.hydrateEntityState(Loader.java:1794)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1767)
    at org.hibernate.loader.Loader.getRow(Loader.java:1615)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:745)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:1008)
    at org.hibernate.loader.Loader.doQuery(Loader.java:964)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2815)
    ... 11 more
Caused by: com.mysql.cj.exceptions.DataConversionException: Unsupported conversion from TIMESTAMP to java.lang.Long
    at com.mysql.cj.result.DefaultValueFactory.unsupported(DefaultValueFactory.java:70)
    at com.mysql.cj.result.DefaultValueFactory.createFromTimestamp(DefaultValueFactory.java:82)
    at com.mysql.cj.protocol.a.MysqlTextValueDecoder.decodeTimestamp(MysqlTextValueDecoder.java:79)
    at com.mysql.cj.protocol.result.AbstractResultsetRow.decodeAndCreateReturnValue(AbstractResultsetRow.java:87)
    at com.mysql.cj.protocol.result.AbstractResultsetRow.getValueFromBytes(AbstractResultsetRow.java:241)
    at com.mysql.cj.protocol.a.result.ByteArrayRow.getValue(ByteArrayRow.java:91)
    at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1290)
    ... 29 more
配置为:

  • 爪哇8
  • Mysql8
  • JPA2.1
  • 春天5

请检查诱惑实体类和db表结构。如果您有任何与之相关的未匹配项,那么您必须相应地映射它。如果有什么问题的话,事情就不会发生了


例如,-java.util.Date列类型必须与数据库中的Date类型列匹配。

请描述表TENTRABLE如果day存储为timestamp,则需要更改hqlno,列定义为日期列类型。您使用的是多年前被JSR 310中定义的java.time类取代的糟糕的日期时间类。对于没有时间和时区的仅日期值,请使用
LocalDate
。我建议不要使用
SimpleDateFormat
date
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。而是使用来自的类。如果您在MySQL中的数据类型是
时间戳
(在MySQL中定义了一个int时间点),那么您应该能够将其检索到
OffsetDateTime
Instant
中。请参阅。实际上,
java.util.Date
类表示UTC中的一个时刻,因此它将时间戳的标准SQL类型与时区相匹配。