Spring Boot 项目由于架构验证而无法运行:缺少序列 [hibernate_sequence]

2022-09-04 02:21:21

当我尝试运行Spring Boot和Hibernate应用程序时,我发现它正在掉落,因为:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing sequence [hibernate_sequence]

但我不明白为什么这是因为我没有使用Hibernate序列。我的表,在Apache Derby中,如下:

CREATE TABLE TEAM (
  TEAM_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),    
  NAME VARCHAR(50) NOT NULL,    
  CONSTRAINT PK_TEAM PRIMARY KEY(Team_Id)
);

CREATE TABLE PLAYER (
  PLAYER_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),    
  NAME VARCHAR(50) NOT NULL,  
  NUM INTEGER NOT NULL, 
  POSITION VARCHAR(50) NOT NULL,    
  TEAM_ID INTEGER, 
  CONSTRAINT PK_PLAYER PRIMARY KEY(PLAYER_ID),
  CONSTRAINT FK_PLAYER FOREIGN KEY(TEAM_ID) REFERENCES TEAM(TEAM_ID)
);

我的应用程序的文件是:application.properties

# Hibernate table generation.
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
spring.jpa.show-sql=true    

# Apache Derby settings
spring.datasource.driverClassName=org.apache.derby.jdbc.ClientDriver
spring.datasource.url=jdbc:derby://localhost:1527/Library
spring.datasource.username=username
spring.datasource.password=password`

涉及的两个Java类是:

@Entity
@Table(name = "TEAM")
public class Team {

    @Id
    @Column(name = "TEAM_ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer teamId;

    @Column(name = "NAME")
    private String name;

    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.EAGER,
            mappedBy = "team")
    private List<Player> players;

和:

@Entity
@Table(name = "PLAYER")
public class Player {

    @Id
    @Column(name = "PLAYER_ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer playerId;

    @Column(name = "NAME")
    private String name;

    @Column(name = "NUM")
    private int num;

    @Column(name = "POSITION")
    private String position;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TEAM_ID", nullable = true)
    private Team team;

任何人都可以建议我错在哪里吗?

Maven 依赖关系包括:

<dependencies>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.14.2.0</version>
        </dependency>      
  </dependencies>

答案 1

您正面临此问题,因为缺少序列 。您可以使用 在数据库上手动创建序列。有关在 中创建序列的详细信息,请访问链接hibernate_sequencecreate sequence <schema>.hibernate_sequenceDerby


答案 2

点击此问题,以下是我的搜索结果:

  1. 如果您在 java bean 中使用,则默认情况下休眠使用序列。GenerationType.AUTOhibernate_sequence

    因此,一种选择是通过以下方式在数据库中创建此序列:

    create sequence <schema>.hibernate_sequence

  2. 或者你可以在你的java bean源代码中使用,这不需要这样的序列。@GeneratedValue(strategy = GenerationType.IDENTITY)

    引用 Java Persistence/Identity:

    标识排序使用数据库中的特殊标识列,以允许数据库在插入对象行时自动为对象分配 ID。许多数据库都支持标识列,例如 MySQL、DB2、SQL Server、Sybase 和 Postgres。Oracle 不支持 IDENTITY 列,但可以通过使用序列对象和触发器来模拟这些列。

延伸阅读:

生成类型.AUTO 与生成类型.处于休眠状态


推荐