【问题标题】:Postgres not creating tables properly with hibernate?Postgres没有用hibernate正确创建表?
【发布时间】:2016-10-25 03:06:09
【问题描述】:

我已将我的休眠应用程序设置为使用以下属性:

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

我还创建了一个 postgres 数据库 users,但是当我运行我的应用程序时,它会抛出所有这些不成功的 alter table 错误?!

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 使用 postgres?我也在我的 postgres 服务器上设置了postgis

我删除了 Point 声明,它仍然抛出相同的错误...所以这不是问题。

【问题讨论】:

    标签: hibernate postgresql spring-boot


    【解决方案1】:

    事实证明,postgres 中不能有一个名为 user 的表。因此,只需将表名从 user 更改为 user_entity 即可解决此问题。

    【讨论】:

    • @MatheusAraujo 我会说这是在不直观的软件中需要的知识,而这些软件总是很糟糕的软件。经验不能成为程序员在编写不够直观的软件时所浪费的时间。
    • 你也不能有名为 user 的列!
    • @KarlRichter 至少在 2020 年这似乎不是真的。这对我有用,没有任何问题。 CREATE TABLE if not exists mynamespace.user(id text, "user" text ); 不过,结合 JPA,我遇到了与问题中所示相同的问题
    【解决方案2】:

    由于接受的答案似乎不是最新的,我想提出不同的解决方案:

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

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

    顺便说一句:名为“授权”的实体也会出现同样的问题

    虽然这适用于当前的 postgres / hibernate 版本,但我宁愿建议将实体命名为 user_entity 或 users。

    【讨论】:

      【解决方案3】:

      如果您有关系字段,只需使用 CascadeType.All:

      @ManyToMany(fetch = EAGER, cascade = CascadeType.ALL)
      private Collection<Role> roles = new ArrayList<>();
      

      【讨论】:

        猜你喜欢
        • 2015-07-07
        • 2018-07-06
        • 2016-10-29
        • 2019-06-28
        • 2011-01-08
        • 2012-02-17
        • 2017-07-22
        • 1970-01-01
        • 2022-01-21
        相关资源
        最近更新 更多