微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我不知道出了什么问题.这两个表似乎都是在Psql中正确创建的.

Comment.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

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

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

解决方法:

你错过了@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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐