1

I had an entity with variable created type Date

public class UserEntity {
...
    @Basic(optional = false)
    @Column(name = "created", nullable = false, updatable = false)
    @CreatedDate
    private Date created;
...
}

the table created a column with the type TIMESTAMP and correctly added the object to the table

[EL Fine]: sql: 2018-05-02 21:54:25.578--ClientSession(210949780)--Connection(1559972721)--INSERT INTO users (activation_token, avatar_id_in_cloud, avatar_provider, created, email, email_change_token, enabled, entity_version, modified_date, new_email, password, unique_id, username) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [null, null, null, [here created ==> 2018-05-02 21:54:25.279 <== here created, TkM2Gs9Hrd@gmail.com, null, true, 1, B@6f8060ac, null, $2a$12$/MwdBoXwBgHlxnsicVW.w.UWdYo6ulLH87aZtSxhdbBYA.9QacUXy, 74a45e29-c1f8-40dc-ac25-a7196d7870b4, JonkiPro]

however, according to Java 8, I changed the Date type to Instant.

public class UserEntity {
{
    @Basic(optional = false)
    @Column(name = "created", nullable = false, updatable = false)
    @CreatedDate
    private Instant created;
...
}

Now it automatically creates a table with a LONGVARBINARY created column, the generated command looks like this

[EL Fine]: sql: 2018-05-02 21:54:25.578--ClientSession(210949780)--Connection(1559972721)--INSERT INTO users (activation_token, avatar_id_in_cloud, avatar_provider, created, email, email_change_token, enabled, entity_version, modified_date, new_email, password, unique_id, username) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [null, null, null, [here created ==> B@6f8060ac <== here created, TkM2Gs9Hrd@gmail.com, null, true, 1, B@6f8060ac, null, $2a$12$/MwdBoXwBgHlxnsicVW.w.UWdYo6ulLH87aZtSxhdbBYA.9QacUXy, 74a45e29-c1f8-40dc-ac25-a7196d7870b4, JonkiPro]

instead of the date there is a string B@6f8060ac, and in the database it is already written as aced00057372000d6a6176612e74696d652e536572955d84ba1b2248b20c00007870770d02000000005aea177110a133c078. What is happening with the date? I'm using the H2 base and EclipseLink.

JONKI
  • 535
  • 5
  • 10
  • 24
  • See https://stackoverflow.com/a/23718471/303810. – lexicore May 02 '18 at 22:01
  • Agree @lexicore. Eclipselink 2.7 implements JPA 2.2 officjally (so Java 8 date time). JPA 2.0 (Eclipselink 2.5/2.6) use converter https://stackoverflow.com/questions/46036493/spring-data-jpa-with-java-8-localdatetime/46036781#46036781 – Jacek Cz May 03 '18 at 14:08

1 Answers1

1

It seems that Instant type isn't supported by JPA, not even the latest version 2.2 which supports most of the new time types.

Instant type is supported by Hibernate and DataNucleus, which map it automatically to a TIMESTAMP. But this isn't true for EclipseLink, which treat it as any other object and uses serialization to store it to the database.

The JPA 2.2 spec lists only the following types treated in a special way:

Java primitive types, java.lang.String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime, java.time.OffsetTime, java.time.OffsetDateTime

All others, including Instant and ZonedDateTime are supported as any other Serialiazable type, unless the implementation does more than the specification requires (Hibernate and DataNucleus do, EclipseLink doesn't). This will hopefully get fixed in the future in EclipseLink and the next version of the JPA spec.

OndroMih
  • 7,280
  • 1
  • 26
  • 44