java - Spring Boot 和 Postgres : relation "sub_comment" does not exist

标签 java postgresql hibernate spring-boot spring-data-jpa

我有两个实体:CommentSubComment。一个 Comment 可以有多个 SubComment。我正在尝试与 Hibernate 建立一对多/多对一的双向关系。

不知道哪里出了问题。这两个表似乎都是在 PSQL 中正确创建的。

评论.java

import javax.persistence.*;
import java.util.Set;

@Entity
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column
    private String text;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "comment")
    private Set<SubComment> subComment;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

子评论.java

import javax.persistence.*;

@Entity
public class SubComment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String text;

    @ManyToOne
    private Comment comment;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }
}

我收到这个错误:

Error executing DDL via JDBC StatementCaused by: org.postgresql.util.PSQLException: ERROR: relation "sub_comment" does not exist

Hibernate: create table "user" (id  bigserial not null, email varchar(255), name varchar(255), username varchar(255), primary key (id))
Hibernate: create table comment (comment_id  bigserial not null, text varchar(255), primary key (comment_id))
Hibernate: create table sub_comment (sub_comment_id  bigserial not null, text varchar(255), comment_comment_id int8, primary key (sub_comment_id))
Hibernate: alter table sub_comment add constraint FK87789n34vmns9eeyw6jgc5ghp foreign key (comment_comment_id) references comment

应用程序属性

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.data-username=username
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

最佳答案

您错过了 @JoinColumn。由于基于字段的访问,您将收到另一个错误。改用基于属性的访问:

import javax.persistence.*;

@Entity
@Table(name = "subcomment")
public class SubComment implements Serializable {

    private static final long serialVersionUID = -3009157732242241606L;

    private long id;
    private String text;
    private Comment comment;

    @Id
    @Column(name = "sub_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "sub_text")
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @ManyToOne
    @JoinColumn(name = "sub_fk_c_id", referencedColumnName = "c_id") // here the exact field name of your comment id in your DB
    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }
}

也在这里进行更改:

import javax.persistence.*;

@Entity
@Table(name = "comment")
public class Comment implements Serializable {

    private static final long serialVersionUID = -3009157732242241606L;    

    private long id;
    private String text;
    private Set<SubComment> subComment = new HashSet<>();

    @OneToMany(mappedBy = "comment", targetEntity = SubComment.class)
    public Set<SubComment> getSubComment() {
        return subComment;
    }

    public void setSubComment(Set<SubComment> subComment) {
        this.subComment = subComment;
    }

    @Id
    @Column(name = "c_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "c_text")
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

将以下内容粘贴到您的 application.properties 文件中:

spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.hibernate.ddl-auto=create

在你的 pom.xml 文件中粘贴这些:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

如需进一步引用,请参阅 this stackoverflow post .

关于java - Spring Boot 和 Postgres : relation "sub_comment" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50247894/

相关文章:

sql - PostgreSQL:获取双括号之间的字符串

java - 使用 persistence.xml 创建 Hibernate SessionFactory

java - Hibernate:使用外键插入数据

java - Oracle JVM 8 : when Codecache flushing is enabled, 刷新了多少?

java - 是否可以确定 Android 位图上 png 压缩的类型?

sql - 选择其他列的值相等的对

java - 将 SQL 更改为条件或 HQL

java - 有没有办法制作一个可以在 Java 中使用 [] 的自定义类,类似于数组?

java - 如何在LWJGL 3中使用WGLARB?

sql - 如何调试 postgresql TRUNCATE 时间的差异?