postgresql 涉及分区表+ Sping Boot 的问题:GenerationTarget遇到异常接受命令:执行DDL“创建表”时出错

rqmkfv5c  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(204)

我正在使用Postgres和Sping Boot 处理分区表。我设法创建了分区,在我执行的测试中,GET和POST方法按预期工作。
但是,我需要帮助解决启动应用程序时发生的以下错误:

**GenerationTarget遇到异常接受命令:通过JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException执行DDL“create table example(date_cd date not null,id int 4 not null,name varchar(255),primary key(date_cd,id))”时出错。通过org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]**通过JDBC语句执行DDL“create table example(date_cd date not null,id int 4 not null,name varchar(255),primary key(date_cd,id))”时出错

我在错误出现之前所做的一步一步:
我创建了主Model,它将引用分区表:

@Entity
@Table(name = "example")
@IdClass(ExampleId.class)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Example {

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

    @Id
    private LocalDate date_cd;

    private String name;

}

我创建了复合ID类。

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

    private static final long serialVersionUID = 1L;

    private int id;

    private LocalDate date_cd;

}

然后我启动了应用程序,一切正常运行。
之后,我暂停了应用程序,并按照Postgres文档对“example”表进行了分区:https://www.postgresql.org/docs/current/ddl-partitioning.html螺纹:5.11.2.1.示例
我的表看起来像这样:

CREATE TABLE IF NOT EXISTS public.example
(
    date_cd date NOT NULL,
    id integer NOT NULL,
    name character varying(255) COLLATE pg_catalog."default",
    CONSTRAINT example_pkey PRIMARY KEY (date_cd, id)
) PARTITION BY RANGE (date_cd);)

因此,我再次启动了应用程序,并在Sping Boot 日志中打印了以下错误:

WARN 20616 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl: 
GenerationTarget encountered exception accepting command : Error executing DDL "create table example 
(date_cd date not null, id int4 not null, name varchar(255), primary key (date_cd, id))" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table example 
(date_cd date not null, id int4 not null, name varchar(255), primary key (date_cd , 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]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:562)
~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at
org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:507)
~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]

我的application.properties看起来像这样:

spring.datasource.url=jdbc:postgresql://localhost:5432/example
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

我知道将'spring.jpa.hibernate.ddl-auto'更改为'none'可以解决这个问题,但这不是我想要的解决方案,因为我不想手动管理所有数据库更改。

z9zf31ra

z9zf31ra1#

我通过将Sping Boot 更新到最新版本并在pom.xml中添加以下依赖项来解决它:

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

相关问题