Postgresql GeneratedValue无法识别现有的hibernate_序列
我有这个用户类Postgresql GeneratedValue无法识别现有的hibernate_序列,postgresql,hibernate,spring-boot,spring-data-jpa,hibernate-mapping,Postgresql,Hibernate,Spring Boot,Spring Data Jpa,Hibernate Mapping,我有这个用户类 @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
@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_序列,以便继续创建用户表
我尝试查看所使用的数据库,并且自动创建了hibernate_序列-因此,不应该产生错误
编辑1
我尝试不使用序列,而是使用GenerationType.IDENTITY
策略,但仍然无法创建表user
编辑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')
}
要使用序列策略生成id,必须首先定义序列生成器。例如:
@Entity
@SequenceGenerator(name="my_seq", initialValue=1, allocationSize=100)
public class User {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_seq")
private Long id;
//...
}
更多信息如下。设置您的
hibernate.hbm2ddl=update
,然后将创建hibernate\u序列。我尝试过使用它,但它似乎没有任何作用。日志仍然显示相同的内容,并且未创建my_seq
。请检查您的“资源”文件夹-其中可能有schema*.sql
文件?M.Deinum是对的。这是由于postgres的用户
关键字造成的。它与序列无关user
是postgres中的保留关键字。您必须在用户
实体上使用@table
关键字明确定义另一个表名。谢谢。我试着把用户换成另一个词,效果很好。这是正确的答案。