1

All,

I am using Spring, Hibernate and MySQL to persist some records to a queue which I process later.

Since I am using Java 8, the recommendation is to use JodaTime library rather than Java SQL Date c.f. here

Model

    @Entity
@Table(name = "order_job_queue")
public class CancelReward implements Serializable {
    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "processed_state")
    private String processedState;

    @Column(name = "order_reference_id")
    private int orderReferenceId;

    @Column(name = "creation_date", columnDefinition = "DATETIME DEFAULT")
    private DateTime creationDate;

    @Column(name = "last_checked_date", columnDefinition = "DATETIME DEFAULT")
    private DateTime lastCheckedDate; ...

@PrePersist
protected void onCreate() {
    creationDate = new DateTime().toDateTime();
}

@PreUpdate
protected void onUpdate() {
    lastCheckedDate = new DateTime().toDateTime();
}

Table

`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`processed_state` VARCHAR(45) NOT NULL,
`order_reference_id` INT UNSIGNED NOT NULL,
`creation_date` DATETIME NOT NULL,
`last_checked_date` DATETIME NOT NULL,

Dependency Versions

  • Spring Framework - 4.3.3
  • Hibernate - 4.3.5
  • Hibernate JPA2 API - 1
  • MySQL Connector - 5.1.40

When I try to save an entity I get this stack trace complaining about truncation.

Data truncation: Incorrect datetime value: '��' for column 'creation_date' at row 1 Error while processing request: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

I've tried using Java SQL Date as well, not sure how to fix this one.a

Community
  • 1
  • 1
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119

1 Answers1

1

Try using the standard date in java:

import java.util.Date;

java.sql.Timestamp should also work but I never tried it myself.

SergeiBednar
  • 370
  • 1
  • 10