Spring 中破裂的关系

Broken relationships in Spring

我有三个实体:

@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "author")
public class Author extends IdEntity {

    private String firstName;
    private String lastName;

    @Email(message = "Provide a valid email")
    @NotEmpty(message = "Email cannot be empty")
    private String email;

    private String userName;

    @Length(min = 5, message = "XD")
    @NotEmpty(message = "Password cannot be empty")
    private String password;
    private String profilePic;
    private String description;
    @CreationTimestamp
    private Date createdAt;

    @JsonManagedReference
    @OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "author")
    @Transient
    private Set<Post> posts;

    public Post getPostById(Long postId) {
        for (Post post: posts)
            if (post.getId().equals(postId))
                return post;
            return null;
    }

    public Author(String firstName, String lastName, @Email(message = "Provide a valid email") @NotEmpty(message = "Email cannot be empty") String email, String userName, @Length(min = 5, message = "XD") @NotEmpty(message = "Password cannot be empty") String password, String profilePic, String description, Date createdAt) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.userName = userName;
        this.password = password;
        this.profilePic = profilePic;
        this.description = description;
        this.createdAt = createdAt;
    }

}

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "comment")
public class Comment extends IdEntity {

    private String content;
    @CreationTimestamp
    private Date createdAt;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "author_id",
            nullable = false,
            foreignKey = @ForeignKey(
                    name = "fk_authors_id"
            ))
    @ToString.Exclude
    @NotNull
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Author author;

    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "post_id",
            nullable = false,
            foreignKey = @ForeignKey(
                    name = "fk_posts_id"
            ))
    @ToString.Exclude
    @NotNull
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private Post post;

}


@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "post")
public class Post extends IdEntity {

    @Length(min = 5, message = "Your title must have at least 5 characters")
    @NotEmpty(message = "Please provide title")
    private String title;
    private String content;
    @CreationTimestamp
    private LocalDateTime createdAt;
    //private LocalDateTime updatedAt;
    private int likes;
    private int views;

    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "author_id",
            nullable = false,
            foreignKey = @ForeignKey(
                    name = "fk_authors_id"
            ))
    @ToString.Exclude
    @NotNull
    private Author author;

    @JsonManagedReference
    @OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "post", cascade = CascadeType.ALL)
    @Transient
    private Collection<Comment> comments;


    //Section, Category, Tags, featured, published
    //Add other reactions

}

每次我尝试添加 Post。它应该添加到 Post table 并自动添加到 Author table,但没有任何反应。与双向映射不同,它看起来更像是 OneToMany 映射。我不知道该如何解决。这是损坏模块的源代码:github.com/Wardenclyffee/Wardenclyffe/tree/comment-posting/server

如果我对你的理解是正确的,你想在添加帖子时添加作者。在那种情况下,您需要通过指定级联类型来告知 Hibernate:

@JsonBackReference
@ManyToOne(
        fetch = FetchType.LAZY, 
        optional = false,
        cascade = CascadeType.PERSIST)
@JoinColumn(
        name = "author_id",
        nullable = false,
        foreignKey = @ForeignKey(
                name = "fk_authors_id"
        ))
@ToString.Exclude
@NotNull
private Author author;

有关 Hibernate 中的级联类型的更多信息,请查看 Web。资源丰富。

此外,在作者实体中为帖子集设置 @Transient 注释似乎是错误的。我不知道会发生什么,但我会删除它,因为瞬态字段用于不应存储在数据库中的字段,但在这种情况下,Hibernate 将正确处理关联。