Problema envolvendo tabela particionada + Spring Boot: GenerationTarget encountered exception accepting command : Error executing DDL "create table

Estou trabalhando com tabelas particionadas usando Postgres e Spring Boot, consegui fazer a partição e nos testes que eu fiz o método GET e POST está funcionando do jeito que eu queria, porém gostaria de ajuda para resolver o seguinte erro, que é retornado quando eu inicio a aplicação:

GenerationTarget encountered exception accepting command : Error executing DDL “create table teste (data date not null, id int4 not null, nome varchar(255), primary key (data, id))” via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL “create table teste (data date not null, id int4 not null, nome varchar(255), primary key (data, id))” via JDBC Statement at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]

O passo a passo do que eu fiz antes do erro aparecer:

Criei o Model principal, que fará referência a tabela particionada:

    @Entity
    @Table(name = "teste")
    @IdClass(TesteId.class)
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public class Teste {

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "teste_id_seq")
        @SequenceGenerator(name = "teste_id_seq", sequenceName = "teste_id_seq", initialValue = 1, allocationSize = 1)
        private int id;

        @Id
        private LocalDate data;

        private String nome;

    }

Criei a classe do id composto:

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    public class TesteId implements Serializable {

        private static final long serialVersionUID = 1L;

        private int id;

        private LocalDate data;

    }

E então iniciei a aplicação, tudo rodou normalmente.

Após isso pausei a aplicação e particionei a tabela ‘teste’ seguindo a documentação do Postgres: PostgreSQL: Documentation: 15: 5.11. Table Partitioning Tópico: 5.11.2.1. Example

E minha tabela ficou da seguinte forma:

    CREATE TABLE IF NOT EXISTS public.teste
    (
        data date NOT NULL,
        id integer NOT NULL,
        nome character varying(255) COLLATE pg_catalog."default",
        CONSTRAINT teste_pkey PRIMARY KEY (data, id)
    ) PARTITION BY RANGE (data);

Então eu iniciei a aplicação novamente e o seguinte erro foi impresso no log do Spring Boot:

   WARN 16668 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget             
    encountered exception accepting command : Error executing DDL "create table teste (data 
    date not null, id int4 not null, nome varchar(255), primary key (data, id))" via JDBC 
    Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL 
    "create table teste (data date not null, id int4 not null, nome varchar(255), primary 
    key (data, id))" via JDBC Statement at 
    org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetTo
    Database.java:67) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at 
    org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigra
    tor.java:562) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at 
    org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigr
    ator.java:507) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]

Meu application.properties está assim:

    spring.datasource.url=jdbc:postgresql://localhost:5432/teste
    spring.datasource.username=postgres
    spring.datasource.password=postgres
    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.defer-datasource-initialization=true
    spring.datasource.driver-class-name=org.postgresql.Driver

Eu sei que alterar o ‘spring.jpa.hibernate.ddl-auto’ para ‘none’ resolveria o problema, porém não é a solução que eu desejo, já que não quero gerenciar manualmente todas as alterações no banco de dados.

Posta os logs inteiros para nós?

Log completo:

2023-05-30 12:04:37.007  INFO 10064 --- [           main] b.c.p.TesteApplication  
    : Starting TesteApplication using Java 17.0.3.1 on DESKTOP-23GKIR0 with PID 10064 (C:\Users\...\back-end\target\classes started by BridgeLabs in C:\Users\...\back-end)
2023-05-30 12:04:37.031  INFO 10064 --- [           main] b.c.p.TesteApplication  
    : No active profile set, falling back to default profiles: default
2023-05-30 12:04:39.439  INFO 10064 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-30 12:04:39.846  INFO 10064 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 350 ms. Found 2 JPA repository interfaces.   
2023-05-30 12:04:42.455  INFO 10064 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-05-30 12:04:42.505  INFO 10064 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-30 12:04:42.521  INFO 10064 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.54]
2023-05-30 12:04:42.843  INFO 10064 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]   
    : Initializing Spring embedded WebApplicationContext
2023-05-30 12:04:42.893  INFO 10064 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5696 ms
2023-05-30 12:04:44.191  INFO 10064 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-05-30 12:04:44.465  INFO 10064 --- [           main] org.hibernate.Version
    : HHH000412: Hibernate ORM core version 5.4.28.Final
2023-05-30 12:04:44.997  INFO 10064 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-05-30 12:04:45.256  INFO 10064 --- [           main] com.zaxxer.hikari.HikariDataSource   
    : HikariPool-1 - Starting...
2023-05-30 12:04:45.671  INFO 10064 --- [           main] com.zaxxer.hikari.HikariDataSource   
    : HikariPool-1 - Start completed.
2023-05-30 12:04:45.859  INFO 10064 --- [           main] org.hibernate.dialect.Dialect        
    : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2023-05-30 12:04:48.356  WARN 10064 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl 
    : GenerationTarget encountered exception accepting command : Error executing DDL "create table teste (data date not null, id int4 not null, nome varchar(255), primary key (data, id))" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table teste (data date not null, id int4 not null, nome varchar(255), primary key (data, id))" via JDBC 
Statement
        at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]      
        at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:318) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.12.jar:5.3.12]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.12.jar:5.3.12]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.12.jar:5.3.12]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.12.jar:5.3.12]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.12.jar:5.3.12]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.12.jar:5.3.12]     
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.12.jar:5.3.12]        
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.12.jar:5.3.12]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.12.jar:5.3.12]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.12.jar:5.3.12]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.6.jar:2.5.6]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.6.jar:2.5.6]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.6.jar:2.5.6]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.6.jar:2.5.6]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.6.jar:2.5.6]
        at br.com.teste.TesteApplication.main(TesteApplication.java:10) ~[classes/:na]
Caused by: org.postgresql.util.PSQLException: ERROR: relation "teste" already exists
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2565) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2297) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284) ~[postgresql-42.2.24.jar:42.2.24]
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:279) ~[postgresql-42.2.24.jar:42.2.24]
        at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-4.0.3.jar:na]
        at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
        ... 34 common frames omitted

2023-05-30 12:04:48.434  INFO 10064 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator   
    : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-30 12:04:48.458  INFO 10064 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-30 12:04:50.076  WARN 10064 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 
2023-05-30 12:04:51.902  INFO 10064 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-30 12:04:51.934  INFO 10064 --- [           main] b.c.p.TesteApplication  
    : Started TesteApplication in 15.738 seconds (JVM running for 16.744)

Aqui está o problema.

Mas qual seria o motivo e solução?

Porque pelo que eu entendi o hibernate tenta criar a tabela ‘teste’ novamente. Mas isso só ocorre com a tabela particionada

Consegui resolver atualizando o Spring Boot para a última versão e adicionando a seguinte dependência no pom.xml:

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>6.2.3.Final</version>
		</dependency>