【问题标题】:Relation does not exist for Spring EntitySpring Entity 的关系不存在
【发布时间】:2022-01-08 07:28:10
【问题描述】:
import javax.persistence.*;

    @Table(name = "users")
    @Entity
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "userID")
        private long userID;
    }
        
    @Table(name = "subscriptions")
    @Entity
    public class Subscription {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "subscriptionID")
        private long subscriptionID;
    
        @ManyToOne
        @JoinColumn(name="userID")
        private User user;
    
    }

此代码崩溃:

引起:org.postgresql.util.PSQLException:错误:关系 “订阅”不存在

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL方言

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect spring.jpa.hibernate.ddl-auto=create-drop

spring.datasource.driver-class-name=org.postgresql.Driver

代码适用于

 import javax.persistence.*;
    
        @Table(name = "users")
        @Entity
        public class User {
        
            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            @Column(name = "userID")
            private long userID;
        }
            
        @Table(name = "subscriptions")
        @Entity
        public class Subscription {
        
            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            @Column(name = "subscriptionID")
            private long subscriptionID;
        
        }

我该如何解决?

【问题讨论】:

  • 能否请您检查一下是否创建了表,或者Hibernate创建表时是否出现异常?
  • GenerationTarget 遇到异常接受命令:通过 JDBC 语句执行 DDL“alter table subscriptions drop constraint FK5ju201bkc08up0b1x2x51mblu”时出错
  • @SimonMartinelli 谢谢你的帮助

标签: java spring hibernate jpa


【解决方案1】:

只需使用spring.jpa.hibernate.ddl-auto=update 而不是create-drop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 2021-08-17
    • 2018-12-02
    • 2020-06-17
    • 2021-04-14
    • 2016-12-18
    • 2020-03-12
    • 2019-01-12
    相关资源
    最近更新 更多