Kohei Nozaki's blog 

Using JPA 2.1 AttributeConverter against Java8 LocalDate / LocalDateTime


Posted on Tuesday Mar 17, 2015 at 01:50PM in JPA


I created an example project using https://weblogs.java.net/blog/montanajava/archive/2014/06/17/using-java-8-datetime-classes-jpa which ran on WildFly 8.2.0.Final (Hibernate 4.3.7) and H2 / Apache Derby database.

the whole project can be obtained from https://github.com/lbtc-xxx/jpa21converter .

You don’t need to define any additional configuration in persistence.xml if you use converters in EE environment. it goes like this:

The converter for LocalDate between DATE

@Converter(autoApply = true)
public class MyLocalDateConverter implements AttributeConverter<java.time.LocalDate, java.sql.Date> {

    @Override
    public java.sql.Date convertToDatabaseColumn(java.time.LocalDate attribute) {
        return attribute == null ? null : java.sql.Date.valueOf(attribute);
    }

    @Override
    public java.time.LocalDate convertToEntityAttribute(java.sql.Date dbData) {
        return dbData == null ? null : dbData.toLocalDate();
    }
}

The converter for LocalDateTime between TIMESTAMP

@Converter(autoApply = true)
public class MyLocalDateTimeConverter implements AttributeConverter<java.time.LocalDateTime, java.sql.Timestamp> {

    @Override
    public java.sql.Timestamp convertToDatabaseColumn(java.time.LocalDateTime attribute) {
        return attribute == null ? null : java.sql.Timestamp.valueOf(attribute);
    }

    @Override
    public java.time.LocalDateTime convertToEntityAttribute(java.sql.Timestamp dbData) {
        return dbData == null ? null : dbData.toLocalDateTime();
    }
}

Entity class

@Entity
public class MySimpleTable implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    private java.time.LocalDateTime someLocalDateTime;
    private java.time.LocalDate someLocalDate;
...

Hibernate produces the DDL against H2 as follows:

create table MySimpleTable (
    id bigint not null,
    someLocalDate date,
    someLocalDateTime timestamp,
    primary key (id)
)

Using converters with @EmbeddedId

Converters doesn’t work with fields that annotated as @Id (see http://stackoverflow.com/questions/28337798/hibernate-fails-to-load-jpa-2-1-converter-when-loaded-with-spring-boot-and-sprin ) but works with @EmbeddedId class.

Entity class:

@Entity
public class MyCompositeKeyTable implements Serializable {
    @EmbeddedId
    private MyCompositeKeyEmbeddable key;
...

Embeddable class:

@Embeddable
public class MyCompositeKeyEmbeddable implements Serializable {
    @Column(nullable = false)
    private java.time.LocalDateTime someLocalDateTime;
    @Column(nullable = false)
    private java.time.LocalDate someLocalDate;
...

Produced DDL:

create table MyCompositeKeyTable (
    someLocalDate date not null,
    someLocalDateTime timestamp not null,
    primary key (someLocalDate, someLocalDateTime)
)



Comments:

Thanks Kohei for this article, I'm updating TightBlog to Java8 time right now, and your "MyLocalDateTimeConverter" now has a proud home in my project. :)

Posted by Glen on June 13, 2016 at 05:15 AM JST #


Hi Glen, I'm glad I could help. thank you for letting me know!

Posted by Kohei on June 13, 2016 at 08:49 PM JST #


Leave a Comment

HTML Syntax: NOT allowed