我有这个用户类
@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')
}
最佳答案
要使用 Sequence 策略生成 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 .
关于postgresql - GeneratedValue 无法识别现有的 hibernate_sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45791023/