小编Jas*_*ain的帖子

Spring Boot & Postgres: relation "sub_comment" does not exist

I have two entities: Comment, and SubComment. A Comment can have multiple SubComments. I'm trying to establish a one to many/many to one bidirectional relationship with Hibernate.

I do not know what is wrong. Both of the tables seem to have been created correctly in 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 …
Run Code Online (Sandbox Code Playgroud)

java postgresql hibernate spring-data-jpa spring-boot

5
推荐指数
1
解决办法
1万
查看次数

Spring Data 和 JPA 中的递归关系?

我的Comment实体是自加入的,有一个subComment集合。

@Entity
public class Comment {

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

@OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL)
private Set<Comment> subComments = new HashSet<>();

@ManyToOne
@JoinColumn(referencedColumnName = "id")
private Comment parentComment;
Run Code Online (Sandbox Code Playgroud)

在我的addComment方法中

public ResponseEntity<Comment> addComment(Comment comment) {
    Comment currComment = commentRepository.save(comment);
    if (currComment.getParentId() != null) {
        Comment parent = commentRepository.findById(currComment.getParentId()).orElse(null);
        if (parent != null) {
            parent.addSubComment(currComment);
            currComment.setParentId(parent.getId());
            currComment.setParentComment(parent);
            commentRepository.save(parent);
        }
    }
    Comment responseComment = commentRepository.save(currComment);
    return ResponseEntity.ok(responseComment);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试建立反向关系(拥有方)时, comment.setParentComment(parent);导致错误

comment.setParentComment(parent); 导致错误: …

java spring hibernate h2

4
推荐指数
2
解决办法
1万
查看次数

标签 统计

hibernate ×2

java ×2

h2 ×1

postgresql ×1

spring ×1

spring-boot ×1

spring-data-jpa ×1