Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot project fails to run because of Schema-validation: missing sequence [hibernate_sequence]

When I try to run a Spring Boot and Hibernate application, I am finding that it is falling over because of:

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

But I cannot see why this is because I am not using Hibernate sequences. My tables, in Apache Derby, are as follows:

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)
);

My application's application.properties file is:

# 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`

And the two Java classes involved are:

@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;

And:

@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;

Can anyone advise where I am wrong?

Maven dependencies are:

<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>
like image 499
Mr Morgan Avatar asked Jun 02 '18 17:06

Mr Morgan


2 Answers

You are facing the issue as there is a missing sequence hibernate_sequence. You can create the sequence manually on your DB using create sequence <schema>.hibernate_sequence. For more info about creating a sequence in Derby please follow the link.

like image 158
Amit Bera Avatar answered Nov 14 '22 11:11

Amit Bera


Hit this issue and below is my searching results:

  1. If you use GenerationType.AUTO in your java bean, then by default hibernate uses hibernate_sequence for the sequence.

    So one option is to create this sequence in the DB by:

    create sequence <schema>.hibernate_sequence

  2. or you can use @GeneratedValue(strategy = GenerationType.IDENTITY) instead in your java bean source code, which does not require such sequence.

    Quoting Java Persistence/Identity:

    Identity sequencing uses special IDENTITY columns in the database to allow the database to automatically assign an id to the object when its row is inserted. Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase and Postgres. Oracle does not support IDENTITY columns but they can be simulated through using sequence objects and triggers.

Further reading:

GenerationType.AUTO vs GenerationType.IDENTITY in hibernate

like image 35
ZhaoGang Avatar answered Nov 14 '22 09:11

ZhaoGang