StephanM
StephanM

Reputation: 1400

Hibernate LocalDateTime: Postgresql schema generation creates columns of type "bytea" in external Tomcat

Im having a problem with hibernate schema generation of my postgresql databse for my webapp which is hosted in a external tomcat (8.5.20). For this i have set the spring.jpa.hibernate.ddl-auto to create-drop in my spring boot app. When im running the app inside my eclipse with the embedded tomcat all LocalDateTime columns are generated as timestamp without time zone which is fine. But when generating the the war file and deploying to a external tomcat with activated schema generation all these columns are generated as type bytea.

I was trying to define global jndi resources (server.xml) linked in context.xml, only context.xml based jndi reosurces and with the spring.data and spring.jpa configuration im having when running in eclipse in my application.properties. But as soon as i deploy the generated war to my external tomcat these columns are generated as bytea.

I also put the postgresql-42.1.1.jar to the tomcat lib folder but also without any success.

These are my settings for my local environemt which work fine:

spring.datasource.url= jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.jpa.hibernate.ddl-auto=create-drop

This is a example of defining the tomcat jndi datasource in context.xml which generates the bytea columns:

<Resource name="jdbc/test" auth="Container"
    type="javax.sql.DataSource" maxActive="20" maxIdle="5" maxWait="10000"
    username="postgres" password="postgres" driverClassName="org.postgresql.Driver"
    url="jdbc:postgresql://localhost:5432/test">
</Resource>

In this case my application properties looks like this:

spring.datasource.jndi-name=java:comp/env/jdbc/test
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.jpa.hibernate.ddl-auto=create-drop

Last i tried is to build and run a generated jar. Columns are again generated of type 'bytea'. So the only variant where the columns are generated as timezones is running the app inside eclipse...

Anybody an idea?

Upvotes: 1

Views: 1189

Answers (1)

Jacek Cz
Jacek Cz

Reputation: 1906

In general should NOT work. And yes, BLOB convention is used. Java8 LocalDateTime isn't officially part of JPA 2.1

Question is WHY one config positive work? I'm almost sure, PostgreSQL has nothing t problem.

Maybe You have newer / older hibernate version? Newer Hibernate versions maybe have 'early release' ? Pure presumption, I'm in latest years Eclipselink user, and Eclipselink does NOT implement.

Generally suggested and good proven way is JPA type converter

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

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

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

EDIT: maybe You have such converter in some JAR, so automatically is forced???

EDIT2: maybe better code and theory by Adam Bien?

http://www.adam-bien.com/roller/abien/entry/new_java_8_date_and

Upvotes: 1

Related Questions