1

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')
}
2
  • 2
    It has nothing to do with the sequence... user is a reserved keyword in postgres. You have to explicitly define another table name with the @Table keyword on your User entity. Commented Aug 21, 2017 at 7:30
  • thanks. I tried changing the User to another word and it works. This is the correct answer. Commented Aug 21, 2017 at 7:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried using that but it seemed like it doesn't do anything. The logs still shows the same and my_seq is not created too.
Check your 'resource' folder - maybe there is a schema*.sql file in it?
M. Deinum is right. It was due to the User keyword of postgres.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.