从 SpringBoot 2.7.18 迁移到 SpringBoot 3.4.0 和 Hibernate 6 后,我在自动更新数据库结构方面遇到问题。 当我设置参数时
spring.jpa.hibernate.ddl-auto=update
我在启动时遇到错误:
[PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Error accessing column metadata: central.discount_code [This connection has been closed.] [n/a]
但在上面我看到其他错误:
09:46:22.380 [main] ERROR o.h.m.internal.MetadataContext - HHH015007: Illegal argument on static metamodel field injection : org.hibernate.envers.DefaultRevisionEntity_#class_; expected type : org.hibernate.metamodel.model.domain.internal.EntityTypeImpl; encountered type : jakarta.persistence.metamodel.MappedSuperclassType
09:46:22.403 [main] INFO o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
09:46:22.898 [main] WARN c.zaxxer.hikari.pool.ProxyConnection - HikariPool-1 - Connection org.postgresql.jdbc.PgConnection@293ecff6 marked as broken because of SQLSTATE(0A000), ErrorCode(0)
org.postgresql.util.PSQLException: ERROR: cannot alter type of a column used by a view or rule
Szczegóły: rule _RETURN on view central.sn_person_portal_max_date depends on column "osoba_created"
09:46:22.903 [main] WARN o.h.t.s.i.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : Error executing DDL "
alter table if exists cn_osoba
alter column osoba_created set data type timestamp(6)" via JDBC [ERROR: cannot alter type of a column used by a view or rule
Szczegóły: rule _RETURN on view central.sn_person_portal_max_date depends on column "osoba_created"]
09:46:22.906 [main] WARN o.h.t.s.i.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : Error executing DDL "
alter table if exists cn_osoba
alter column osoba_rabat set data type numeric(38,2)" via JDBC [This connection has been closed.]
Caused by: org.postgresql.util.PSQLException: This connection has been closed.
我使用的是 PostgreSQL 版本 13.2 和驱动程序版本 42.7.3。
当我使用 Spring 2 时它起作用了。
为什么 Hibernate 将记录类型更改为时间戳(6),预览版本没有这样做?为什么得到“连接已关闭”?怎么解决?
答案在日志中:
cannot alter type of a column used by a view or rule
我已经删除了阻碍对列进行更改的视图。之后 Hibernate 更新了数据库结构并重新创建了视图。由于该应用程序以参数“spring.jpa.hibernate.ddl-auto”启动,设置为“更新”,没有错误。
根据@M.Deinum的建议,我会将此参数设置为“验证”并使用Flyway来更新数据库结构。