关于java:Spring Boot&

Spring Boot & Postgres: relation “sub_comment” does not exist

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

我不知道怎么了。这两个表似乎都是在psql中正确创建的。

注释.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

1
2
3
4
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

应用程序属性

1
2
3
4
5
6
7
8
9
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。由于基于字段的访问,您将得到另一个错误。改用基于属性的访问:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
    }
}

在此也进行更改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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文件中:

1
2
3
4
5
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文件中粘贴以下内容:

1
2
3
4
5
    <dependency>
        <groupId>org.postgresql</groupId>
        postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

如需进一步参考,请参阅此stackoverflow post。


许多子注释属于一个注释,模型应该是这样的(您可以生成的注释ID,例如uuid.randomuuid().toString())

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@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;
...
}

如果使用liquidbase控制数据源:

1
2
3
4
5
6
7
8
9
10
  - changeSet:
      id: 1
      author: author.name
      changes:
      - addForeignKeyConstraint:
          baseColumnNames: comment_id
          baseTableName: sub_comment
          constraintName: fk_comment_subcomment
          referencedColumnNames: comment_id
          referencedTableName: comment