不支持从DATE到java.lang.Integer的转换

问题描述 投票:-2回答:1

因此,我使用Java Ebean库生成了以下模型:

public class Zeit extends BaseModel {



    @Column
    private Date start;

    @Column
    private Date ende;

    @Column
    private int pause;

...

这将导致这些SQL语句:

create table zeit (
  id                            bigint auto_increment not null,
  start                         datetime(6),
  ende                          datetime(6),
  pause                         integer not null,
  version                       bigint not null,
  when_created                  datetime(6) not null,
  when_modified                 datetime(6) not null,
  constraint pk_zeit primary key (id)
);

这个想法是我想将打开和关闭时间记入数据库。基本上可以通过使用

Zeit z = new Zeit((new Date()), null, 0);
z.save();

但是从数据库取回实体时,出现以下错误:

com.mysql.cj.exceptions.DataConversionException: Unsupported conversion from TIMESTAMP to java.lang.Integer

到目前为止,我尝试过

  • 更改数据库中的不同数据类型
  • 在我的代码中使用不同的类型(java.util.Datejava.sql.Date,...]

我也在使用

  • mysql-connector-java:8.0.19
  • io.ebean:12.1.8
java mysql ebean
1个回答
1
投票

错误的数据类型

MySQL中的DATETIME数据类型表示日期和时间,但缺少时区或偏移量的任何概念。因此,这种类型不能表示一个时刻,也不是时间轴上的一点。

此类型的标准SQL等效项为TIMESTAMP WITHOUT TIME ZONE。 Java中的等效项是LocalDateTime

enter image description here

要在标准SQL中记录时刻,请使用TIMESTAMP WITH TIME ZONE。在MySQL中,TIMESTAMP。在Java中,InstantOffsetDateTimeZonedDateTime,对于JDBC 4.2和更高版本,需要OffsetDateTime的支持,而对其他两个的支持是可选的。

OffsetDateTime odt = OffsetDateTime.now() ;
myPreparedStatement.setObject( … , odt ) ;  // Writing to a column of MySQL type `TIMESTAMP`, standard SQL `TIMESTAMP WIH TIME ZONE`.

检索。

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

关于ebean错误,您没有提供足够的详细信息。我们将需要查看它试图实例化到的Java类。

© www.soinside.com 2019 - 2024. All rights reserved.