Search code examples
javahibernatejpajboss

hibernate schema validation failure for LocalDate - found date, but expecting timestamp


I had recently tried my application to deploy from JBoss EAP 7.0 to 7.2.4 and since then I am facing this issue with schema validation for fields with LocalDate mapped to the columns of date types.
The exact failure message is:

{"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"project-name.ear#MyPersistenceUnit\"" => "javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build Hibernate SessionFactory
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build Hibernate SessionFactory
    Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [someDate] in table [SOME_TABLE]; found [date (Types#DATE)], but expecting [timestamp (Types#TIMESTAMP)]"}}

The snippet from schema:

create table SOME_TABLE(
  -- primiary key and other columns
  someDate date
);

The field in the entity:

@Convert(converter = LocalDateConverter.class)
private LocalDate someDate;

The converter

import java.sql.Date;
import java.time.LocalDate;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public class LocalDateConverter
    implements
    AttributeConverter<LocalDate, Date> {

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

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

I found that adding a @Column(columnDefinition = "date") as suggested here solves the issue but my question is:

  1. Do I necessarily need to force to add the columnDefinition in such cases?
  2. The application works well in JBoss 7.0. How can I know what changes introduced in JBoss 7.2.4 which break my application?
  3. Any other recommendation to solve this issue.

This issue is with H2 while connecting with Oracle there is no such issue.


Solution

  • Updated

    Seems you hit a incompatibility of Hibernate with H2 driver while working with DATE in Oracle MODE. See: https://github.com/h2database/h2database/issues/1824

    Besides, JBoss 7.2 now supports JPA 2.2 (See: https://access.redhat.com/articles/113373). In JPA 2.2 support where added for Java8 time classes like LocalDate, so you can remove your converter.

    JPA 2.2 uses JDBC 4.2. Here are the object mappings in JDBC.

    See Table B-4 on JDBC 4.2 spec:

    Table B-4 on JDBC 4.2 spec