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

时间:2018-06-02 17:35:45

标签: java spring hibernate spring-boot

当我尝试运行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>

2 个答案:

答案 0 :(得分:2)

遇到此问题,以下是我的搜索结果:

  1. 如果您在Java Bean中使用GenerationType.AUTO,则默认情况下,休眠模式将hibernate_sequence用于序列。

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

    create sequence <schema>.hibernate_sequence

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

    引用Java持久性/身份:

    身份排序使用数据库中的特殊IDENTITY列来 允许数据库在以下情况下自动为对象分配ID 其行已插入。许多数据库都支持标识列, 例如MySQL,DB2,SQL Server,Sybase和Postgres。甲骨文不 支持IDENTITY列,但可以通过使用进行模拟 排序对象和触发器。

进一步阅读:

GenerationType.AUTO vs GenerationType.IDENTITY in hibernate

答案 1 :(得分:1)

由于缺少序列hibernate_sequence,您将遇到此问题。您可以使用create sequence <schema>.hibernate_sequence在数据库上手动创建序列。有关在Derby中创建序列的详细信息,请按照link进行操作。