【发布时间】: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