我在为托管在外部 tomcat (8.5.20) 中的 web 应用程序生成 postgresql 数据库的 hibernate 模式时遇到问题。
为此,我在我的 spring boot 应用程序中将 spring.jpa.hibernate.ddl-auto
设置为 create-drop
。
当我使用嵌入式 tomcat 在我的 eclipse 中运行应用程序时,所有 LocalDateTime 列都生成为
timestamp without time zone
这很好。
但是当生成 war 文件并部署到具有激活模式生成的外部 tomcat 时,所有这些列都生成为类型
bytea
.
我试图定义在 context.xml 中链接的全局 jndi 资源 (server.xml),只有基于 context.xml 的 jndi 资源以及 spring.data 和 spring.jpa 配置在我的应用程序中运行时在 eclipse 中运行。特性。但是一旦我将生成的 war 部署到我的外部 tomcat,这些列就会生成为 bytea。
我也把 postgresql-42.1.1.jar 放到了 tomcat 的 lib 文件夹,但也没有成功。
这些是我的本地环境设置,工作正常:
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
这是在生成 bytea 列的 context.xml 中定义 tomcat jndi 数据源的示例:
<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>
在这种情况下,我的应用程序属性如下所示:
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
我最后尝试的是构建并运行生成的 jar。再次生成类型为“bytea”的列。因此,列作为时区生成的唯一变体是在 eclipse 中运行应用程序...
有什么想法吗?
最佳答案
一般情况下不应该工作。是的,使用了 BLOB 约定。 Java8 LocalDateTime 不是 JPA 2.1 的正式部分
问题是为什么一个配置积极工作?我几乎可以肯定,PostgreSQL 没有任何问题。
也许您有较新/较旧的 hibernate 版本?较新的 Hibernate 版本可能有“提前发布”?纯粹的假设,我是最近几年的 Eclipselink 用户,而 Eclipselink 没有实现。
通常建议并经过验证的好方法是 JPA 类型转换器
@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());
}
}
编辑:也许你在某些 JAR 中有这样的转换器,所以自动强制???
EDIT2:也许 Adam Bien 的代码和理论更好?
http://www.adam-bien.com/roller/abien/entry/new_java_8_date_and
关于postgresql - hibernate LocalDateTime : Postgresql schema generation creates columns of type "bytea" in external Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45813169/