-2

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
Aien Saidi
  • 159
  • 7
  • 25
  • 2
    Don't add irrelevant tags. Though the app. may have a GUI, it is clear from the question that the problem has nothing to do with Swing. – Andrew Thompson Apr 14 '20 at 08:06
  • @basil-bourque I don't know why did you close the question. The answer you've posted to me does not even speak about the issue I am facing. anyway, I'll look somewhere else. – Aien Saidi Apr 14 '20 at 14:51
  • You are correct, I closed for a Question that does not match close enough. Reopened. – Basil Bourque Apr 14 '20 at 14:55

1 Answers1

1

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.

enter image description here

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    It’s very good information (+1). It’s like it doesn’t explain the issue that the questioner seems to be having with Ebean, though. – Ole V.V. Apr 14 '20 at 15:19
  • thank you for the answer, I've provided more information about the issue. Although it seems that even by using the `LocalDateTIme` it still has the same issue. – Aien Saidi Apr 14 '20 at 16:51
  • @AienSaidi Again, `LocalDateTime` is exactly the wrong data type. Reread my Answer again. – Basil Bourque Apr 14 '20 at 17:37
  • Still the same happens. anyway, I have to change the model. thanks – Aien Saidi Apr 14 '20 at 17:59