Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

persist java LocalDate in MySQL

I'm writing a java client application using: SE 8, MySQL 5.6 (Connector/J 5.1), JPA 2.1. when I try to persist an entity with an ID (int Auto-increment), date (LocalDate). it throw an Exception says:

Internal Exception: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xDF\x03\x06x' for column 'date' at row 1

does MySQL (I mean The Connector) do not support the new Date and Time API or What. if so What Can I do??

@Entity
@Table(schema="app")
public class Run implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;                     //number of connections

    private LocalDate date;
like image 271
usertest Avatar asked Mar 06 '15 10:03

usertest


People also ask

How do you persist LocalDate and LocalDateTime with JPA?

You need to implement the AttributeConverter<LocalDateTime, Timestamp> interface and the converter needs to be annotated with the @Converter annotation. Similar to the LocalDateConverter, the conversion between a LocalDateTime and a java. sql. Timestamp is done with the conversion methods of Timestamp.

What is the difference between LocalDate and LocalDateTime?

LocalDate – represents a date (year, month, day) LocalDateTime – same as LocalDate, but includes time with nanosecond precision.

What is LocalDate NOW () in Java?

now() now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone. This method will return LocalDate based on system clock with default time-zone to obtain the current date.


3 Answers

Registering the custom converter should help you solve your issue

@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements
    AttributeConverter<LocalDate, Date> {
    @Override
    public java.sql.Date convertToDatabaseColumn(LocalDate entityValue) {
        return java.sql.Date.valueOf(entityValue);
    }

    @Override
    public LocalDate convertToEntityAttribute(java.sql.Date databaseValue) {
        return databaseValue.toLocalDate();
    }
}

more about converting the LocalDate info and some more about using the converters

like image 102
Master Slave Avatar answered Sep 29 '22 20:09

Master Slave


AttributeConverter is also generic:

@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements
        AttributeConverter<LocalDate, Date> {
    @Override
    public Date convertToDatabaseColumn(LocalDate entityValue) {
        return java.sql.Date.valueOf(entityValue);
    }

    @Override
    public LocalDate convertToEntityAttribute(Date databaseValue) {
        return databaseValue.toLocalDate();
    }

}
like image 29
RichardK Avatar answered Sep 29 '22 20:09

RichardK


If working with hibernate I just found here that also one can take advantage of Java 8 support shipped in a separate jar file called hibernate-java8.jar. So basically you can just add something like this in your pom and be ready to go:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
</dependency>
like image 24
megalucio Avatar answered Sep 29 '22 20:09

megalucio