简体   繁体   中英

Unsupported conversion from DATE to java.lang.Integer

So, I have the following model generated using Java Ebean library:

package models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.time.LocalDateTime;

@Entity
@Table(name = "Zeit")
public class Zeit extends BaseModel {

    @Column
    private LocalDateTime start;

    @Column
    private LocalDateTime ende;

    @Column
    private int pause;

    @ManyToOne(optional = false)
    private Benutzer benutzer;

    @Column
    private LocalDateTime pauseStart;

    public Zeit() {
    }

    public Zeit(LocalDateTime start, LocalDateTime ende, int pause, LocalDateTime pauseStart) {
        this.start = start;
        this.ende = ende;
        this.pause = pause;
        this.pauseStart = pauseStart;
    }

    public LocalDateTime getStart() {
        return start;
    }

    public void setStart(LocalDateTime start) {
        this.start = start;
    }

    public LocalDateTime getEnde() {
        return ende;
    }

    public void setEnde(LocalDateTime ende) {
        this.ende = ende;
    }

    public int getPause() {
        return pause;
    }

    public void setPause(int pause) {
        this.pause = pause;
    }

    public Benutzer getBenutzer() {
        return benutzer;
    }

    public void setBenutzer(Benutzer benutzer) {
        this.benutzer = benutzer;
    }

    public LocalDateTime getPauseStart() {
        return pauseStart;
    }

    public void setPauseStart(LocalDateTime pauseStart) {
        this.pauseStart = pauseStart;
    }
}

which results in these SQL statements:

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)
);

The idea is that I want to write down the opening and closing time into the db. This is basically possible by using

private void btn_anmeldenActionPerformed(java.awt.event.ActionEvent evt) {
    try {
        Zeit z = new Zeit();
        z.setStart(LocalDateTime.now());
        z.setBenutzer(benutzer);
        z.save();
        btn_anmelden.setText(String.valueOf(z.getStart()));
        btn_anmelden.setEnabled(false);
    } catch (Exception e) {

    }
}

But when getting the entity back from the database using the following:

private void initState() {
    Zeit lastTime = new QZeit().findList().get(0);

    System.out.println(lastTime);
}

I get the error:

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

So far I tried to

  • change different data types in the database
  • use different types in my code ( java.util.Date , java.sql.Date , ...)

I'm also using

  • mysql-connector-java : 8.0.19
  • io.ebean :12.1.8

Wrong data type

The DATETIME data type in MySQL represents a date and a time-of-day but lacks any concept of time zone or offset. So this type cannot represent a moment, is not a point on the timeline.

The standard SQL equivalent of this type is TIMESTAMP WITHOUT TIME ZONE . The equivalent in Java is LocalDateTime .

在此处输入图像描述

To record a moment in standard SQL, use TIMESTAMP WITH TIME ZONE . In MySQL, TIMESTAMP . In Java, Instant , OffsetDateTime , and ZonedDateTime , with support for OffsetDateTime being required for JDBC 4.2 and later while support for the other two is optional.

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

Retrieval.

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

As for the ebean error, you've not provided enough detail. We would need to see the Java class into which it is trying to instantiate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM