gzz
gzz

Reputation: 675

GeneratedValue not recognizing existing hibernate_sequence

I have this User class

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
}

And this is the logs when running the Application

org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: drop table if exists user cascade
org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: syntax error at or near "user"
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: drop sequence hibernate_sequence
org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: sequence "hibernate_sequence" does not exist
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: create table user (id int8 not null, primary key (id))
org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: syntax error at or near "user"

And here is my application.properties

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database=postgresql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver

My understanding on logs is it was looking for hibernate_sequence for it to continue to create User table.

I tried looking into the database used and hibernate_sequence was automatically created - hence, the error should not be produced.

EDIT 1

I tried not to use sequence and use GenerationType.IDENTITY strategy instead but still unable to create table user.

EDIT 2

This is my whole build.gradle if version might be an issue on this too.

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-web')

    compile('org.projectlombok:lombok')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')

    compile('org.postgresql:postgresql:9.3-1101-jdbc4')

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Upvotes: 1

Views: 1454

Answers (2)

Moodi
Moodi

Reputation: 125

Set your hibernate.hbm2ddl=update and the hibernate_sequence will be created.

Upvotes: 1

Cepr0
Cepr0

Reputation: 30289

To use Sequence strategy for generating id it's necessary to define the sequence generator first. For example:

@Entity
@SequenceGenerator(name="my_seq", initialValue=1, allocationSize=100)
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_seq")
    private Long id;

    //...
} 

More info is here.

Upvotes: 2

Related Questions