0

After migration from SpringBoot 2.7.18 to SpringBoot 3.4.0 and Hibernate 6 I have problem with automatic updating database structure. When I set parameter

spring.jpa.hibernate.ddl-auto=update

I got an error on start:

[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]

but above I see other errors:

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.

I'm using PostgreSQL version 13.2 and driver version 42.7.3.
When I was using Spring 2 it worked.
Why Hibernate is changing type of record to timestamp(6), previews versions didn't do it?? Why is get "connection has been closed"?? How to fix it?

5
  • "Why Hibernate is changing type of record to timestamp(6), previews versions didn't do it??" - impossible to say without any code. The whole metadata bit is a red herring in any case, it is the failure to update the model that is the root cause of all problems. Your post also doesn't mention where you are migrating from unless it is Spring 2 which is a massive jump in versions. It's very incomplete.
    – Gimby
    Commented Dec 4 at 9:40
  • 2
    Don't use hibernate to manage your production schema. Use something like Flyway or Liquibase for this. Switch to none as letting hibernate manage this can be dangerous.
    – M. Deinum
    Commented Dec 4 at 10:23
  • @M.Deinum Ok, thanks for advice, I'll look on this two projects. But still I want to get it working on my test database.
    – Olek
    Commented Dec 4 at 12:59
  • That won't happen as the database you are using doesn't allow it. Nor should it change just because yuo upgrade hibernate, if hibernate would manage your schema you are in the danger of loosing data or corrupting data. Instead of update use validate and start using Flyway/Liquibase to manage your schema.
    – M. Deinum
    Commented Dec 5 at 7:56
  • @M.Deinum thanks for advice, I'll set parameter to validate and start using probably Flyway. I've found cause of problem, there was a view which I had to drop to be able to alter columns.
    – Olek
    Commented Dec 6 at 8:04

1 Answer 1

0

The answer was in log:

cannot alter type of a column used by a view or rule

I've drop the view which was blocking doing alter on colums. After that Hibernate updated database structure and I recreate the view. Since that application starts with parameter "spring.jpa.hibernate.ddl-auto" set to "update" without erros.
According to @M.Deinum advice I'll set this parameter to 'validate' and use Flyway to database structure updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.