Hibernate 创建一个新序列而不是使用现有序列并失败

问题描述 投票:0回答:2

我有一个预先存在的 Postgres 数据库,其中包含一堆表。每个表都有一个带有序列的 ID 列。 在我的实体类中,我有这样的东西:

@Entity
@Table(name = "plan_block")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Audited
public class Block {
    @Id
    @GeneratedValue(generator = "plan_block_block_id_seq")
    @SequenceGenerator(allocationSize = 1, name = "plan_block_block_id_seq", sequenceName = "plan_block_block_id_seq")
    @Column(name = "block_id", nullable = false, insertable = false, updatable = false)
    private Long id;
...

该序列确实存在于数据库中。 当我运行该项目时,我得到了

GenerationTarget encountered exception accepting command : Error executing DDL "create sequence "plan_block_block_id_seq" start with 1 increment by 1" via JDBC [ERROR: relation "plan_block_block_id_seq" already exists]

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create sequence "plan_block_block_id_seq" start with 1 increment by 1" via JDBC [ERROR: relation "plan_block_block_id_seq" already exists]

对于我声明的每一个序列。

我尝试向GenerateValue注释添加策略,将allocationSize或initialValue设置为SequenceGenerator注释。

UPD:我想使用现有的序列,因为表中已经有一堆行

java postgresql spring-boot hibernate spring-data-jpa
2个回答
0
投票

它显示“plan_block_block_id_seq”已经存在,因此请尝试将该 seq 删除到您的数据库中。然后再次运行。


0
投票

你可以在application.properties

中尝试
spring.jpa.hibernate.ddl-auto= none

spring.jpa.hibernate.ddl-auto= create

你可以在 application.yml

中尝试
spring:
  jpa:
    hibernate:
      ddl-auto: create

spring:
  jpa:
    hibernate:
      ddl-auto: none
© www.soinside.com 2019 - 2024. All rights reserved.