Java 8 LocalDate in sleep mode incorrectly mapped to TIMESTAMP

I am using Spring boot 1.4.2, which brings hibernate 5.0.11 (JPA 2.1). I want to use Java 8 temporary classes in my entities, and therefore included hibernate-java8.

My entity defines the LocalDate field.

@Entity
@Table(name = "my_alarms_timeline", indexes = {...})
public class MyAlarm {
    ...
    @Column(name = "validity_date")
    @NotNull
    private LocalDate validityDate;
}

I expect this to be matched with a date in my H2 database.

In my DB I declare this as validity_date DATE NOT NULL,

When I try to run the tests, I get the following error:

[INFO] Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: 
    Schema-validation: wrong column type encountered in column [validity_date] in table [my_alarms_timeline]; 
    found [date (Types#DATE)], but expecting [timestamp (Types#TIMESTAMP)]

To my surprise, if I change the definition of the database to validity_date TIMESTAMP NOT NULL,I get an error

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: 
    Schema-validation: wrong column type encountered in column [validity_date] in table [my_alarms_timeline]; 
    found [timestamp (Types#TIMESTAMP)], but expecting [date (Types#DATE)]

This is just the return message from the previous one.

I also tried using instead of turning it on hibernate-java8, AttributeConverter<LocalDate, java.sql.Date>but it gives the same error result.

What should I do so that my local date is correctly matched to a date in the database?

LocalDateTime TIMESTAMP, ...

+7
3

, , columnDefinition="DATE" @Column.

@Entity
@Table(name = "my_alarms_timeline", indexes = {...})
public class MyAlarm {
    ...
    @Column(name = "validity_date", columnDefinition = "DATE")
    @NotNull
    private LocalDate validityDate;
}

, ...

+13

Spring, JPA - :

@Converter(autoApply = true)
public class OffsetDateTimeAttributeConverter implements AttributeConverter<OffsetDateTime, Timestamp> {
    @Override
    public Timestamp convertToDatabaseColumn(OffsetDateTime entityValue) {
        if( entityValue == null )
            return null;

        return Timestamp.from(Instant.from(entityValue));
    }

    @Override
    public OffsetDateTime convertToEntityAttribute(Timestamp databaseValue) {
        if( databaseValue == null )
            return null;

        return OffsetDateTime.parse(databaseValue.toInstant().toString());
    }
}

"timestamp with timezone". @Entity OffsetDateTime. .

+4

, :

@EntityScan(basePackageClasses = { Application.class, Jsr310JpaConverters.class })

Hibernate 5.x, Jsr310JpaConverters.class, Hibernate Spring Java 8 Date Time. (, LocalDate , UTC).

:

@Column(columnDefinition = "DATE")

, , - .

+2

Source: https://habr.com/ru/post/1660338/


All Articles