Postgres无法与Hibernate正常创建表?

7

我已经设置好了我的Hibernate应用程序,使用以下属性:

spring.datasource.url: jdbc:postgresql://localhost:5432/users
spring.datasource.username=james
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=create-drop
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.database: MySql
spring.jpa.database =  postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

我也创建了一个名为users的postgres数据库,但当我运行我的应用程序时,它抛出了很多不成功的改变表错误?!
Hibernate: drop table if exists user cascade
2016-06-23 10:46:05.406 ERROR 3364 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: drop table if exists user cascade
2016-06-23 10:46:05.407 ERROR 3364 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: syntax error at or near "user"
  Position: 22

Hibernate: alter table user_credential drop constraint FK_14ncv1m0gqncrbiagrs4uaqo8
2016-06-23 10:36:36.199 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_credential drop constraint FK_14ncv1m0gqncrbiagrs4uaqo8
2016-06-23 10:36:36.199 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: relation "user_credential" does not exist
Hibernate: alter table user_roles drop constraint FK_5q4rc4fh1on6567qk69uesvyf
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_roles drop constraint FK_5q4rc4fh1on6567qk69uesvyf
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: relation "user_roles" does not exist
Hibernate: alter table user_roles drop constraint FK_g1uebn6mqk9qiaw45vnacmyo2
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_roles drop constraint FK_g1uebn6mqk9qiaw45vnacmyo2
2016-06-23 10:36:36.200 ERROR 3235 --- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport  : ERROR: relation "user_roles" does not exist

在 MySQL 上一切都正常,它可以创建表格而不会出现任何问题。我使用以下属性和 MySQL:

#spring.datasource.url: jdbc:mysql://localhost/users
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.jpa.hibernate.ddl-auto=create-drop
##spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.database: MySql
#spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
##spring.jpa.database-platform=org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect

可能是因为我正在使用vividsolutions的Point数据类型吗?

import com.vividsolutions.jts.geom.Point;
@Column(name = "coords", columnDefinition="Geometry", nullable = true)
private Point location;

我需要做什么才能让Hibernate使用PostgreSQL?我已在我的PostgreSQL服务器上设置了postgis

我删除了Point声明,但它仍然出现相同的错误……所以那不是问题所在。

3个回答

33

原来在 PostgreSQL 中不允许有名为user的表。所以只需要将表名从user更改为user_entity,问题就解决了。


2
@MatheusAraujo 我认为这是在不直观的软件中所需的知识,这总是不好的软件。专业知识不能累积浪费程序员写比可能更不直观的软件的能力。 - Kalle Richter
1
你不能使用名称为"user"的列! - xonya
至少在2020年,这似乎并不正确。 这对我来说没有任何问题。然而,与JPA结合使用时,我遇到了与问题描述相同的问题。 - Tim Malich

0
如果您有关系字段,只需使用CascadeType.All:
@ManyToMany(fetch = EAGER, cascade = CascadeType.ALL)
private Collection<Role> roles = new ArrayList<>();

0

鉴于已接受的答案不够更新,我想提出不同的解决方案:

注意:我猜您正在使用JPA并命名实体为User。还要注意\"

等特殊字符。

@Entity(name = "\"User\"")
public class User implements Serializable {
...
}

顺便提一下:同样的问题也出现在名为“authorization”的实体中

虽然这对于当前的postgres / hibernate版本有效,但我更建议将实体命名为user_entity或users。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接