简体   繁体   English

Spring Boot & Postgres:关系“sub_comment”不存在

[英]Spring Boot & Postgres: relation "sub_comment" does not exist

I have two entities: Comment , and SubComment .我有两个实体: CommentSubComment A Comment can have multiple SubComment s.一个Comment可以有多个SubComment I'm trying to establish a one to many/many to one bidirectional relationship with Hibernate.我正在尝试与 Hibernate 建立一对多/多对一的双向关系。

I do not know what is wrong.我不知道出了什么问题。 Both of the tables seem to have been created correctly in PSQL.这两个表似乎都是在 PSQL 中正确创建的。

Comment.java注释.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;
    }
}

SubComment.java子注释.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;
    }
}

I'm getting this error:我收到此错误:

Error executing DDL via JDBC StatementCaused by: org.postgresql.util.PSQLException: ERROR: relation "sub_comment" does not exist通过 JDBC 语句执行 DDL 时出错引起:org.postgresql.util.PSQLException:错误:关系“sub_comment”不存在

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

application.properties应用程序属性

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

You missed @JoinColumn .你错过了@JoinColumn You will get another error due to field based access.由于基于字段的访问,您将收到另一个错误。 Use Property based access instead:改用基于属性的访问:

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;
    }
}

Also make changes here too:也在这里进行更改:

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;
    }
}

Paste the following in your application.properties file:将以下内容粘贴到您的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

In your pom.xml file paste these:在你的 pom.xml 文件中粘贴这些:

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

For further reference see this stackoverflow post .如需进一步参考,请参阅此 stackoverflow 帖子

I know that this answer is late but maybe it will help someone, their problem is that they have not configured the schema, which is why they are throwing that error.我知道这个答案来晚了,但也许它会帮助某人,他们的问题是他们没有配置架构,这就是他们抛出该错误的原因。

Here is how to configure the schema in postgresql:以下是如何在 postgresql 中配置架构:

spring.jpa.properties.hibernate.default_schema = "his schema"

Many sub comments have belonged to one comment and model should look like that (comment_id you can generate eg UUID.randomUUID().toString() )许多子评论属于一个评论,模型应该看起来像这样(comment_id 你可以生成例如 UUID.randomUUID().toString() )

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

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

@Column(name = "comment_id")
String commentId;

}

@Entity
@Table(name = "sub_comment")
public class SubComment{

...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "comment_id", referencedColumnName = "comment_id")
Comment comment;
...
}

If You use eg liquidbase to control your data source :如果您使用例如liquidbase 来控制您的数据源:

  - changeSet:
      id: 1
      author: author.name
      changes:
      - addForeignKeyConstraint:
          baseColumnNames: comment_id
          baseTableName: sub_comment
          constraintName: fk_comment_subcomment
          referencedColumnNames: comment_id
          referencedTableName: comment

Had a similar problem and my issue was that I was missing referencedColumnName under my SubCategory class.有一个类似的问题,我的问题是我的 SubCategory 类下缺少 referencedColumnName。

Had this有这个

  @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinColumn(name = "category_id, nullable = false)
    private Category category;

Instead of代替

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinColumn(name = "category_id", referencedColumnName = "id", nullable = false)
    private Category category;

so my subcategory table was not being created.所以我的子类别表没有被创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM