【问题标题】:GeneratedValue not recognizing existing hibernate_sequenceGeneratedValue 无法识别现有的 hibernate_sequence
【发布时间】:2018-01-29 04:03:45
【问题描述】:

我有这个用户类

@Entity
public class User {

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

这是运行应用程序时的日志

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"

这是我的 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

我对日志的理解是它正在寻找 hibernate_sequence 以继续创建用户表。

我尝试查看使用的数据库,并自动创建了 hibernate_sequence - 因此,不应产生错误。

编辑 1

我尝试不使用序列并改用GenerationType.IDENTITY策略但仍然无法创建表用户。

编辑 2

这是我的整个 build.gradle 如果版本也可能是这个问题。

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')
}

【问题讨论】:

  • 与序列无关...user是postgres中的保留关键字。您必须在 User 实体上使用 @Table 关键字显式定义另一个表名。
  • 谢谢。我尝试将用户更改为另一个单词并且它有效。这是正确的答案。

标签: postgresql hibernate spring-boot spring-data-jpa hibernate-mapping


【解决方案1】:

要使用序列策略生成 id,首先需要定义序列生成器。例如:

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

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

    //...
} 

更多信息是here

【讨论】:

  • 我尝试使用它,但它似乎没有任何作用。日志仍然显示相同,my_seq 也没有创​​建。
  • 检查你的'resource'文件夹——里面可能有schema*.sql文件?
  • M.迪南是对的。这是由于 postgres 的 User 关键字造成的。
【解决方案2】:

设置您的hibernate.hbm2ddl=update,然后将创建 hibernate_sequence。

【讨论】:

    猜你喜欢
    • 2021-05-13
    • 2017-02-14
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2021-05-31
    • 2023-03-13
    • 2021-05-06
    • 2014-05-04
    相关资源
    最近更新 更多