Spring 关系破裂

ecfsfe2w  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(328)

我有三个实体:

@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表,并自动添加到author表,但这是不可能的。与其说是双向Map,不如说是一对一Map。我不知道怎样才能修好它。下面是损坏模块的源代码:github.com/wardenclyffee/wardenclyffe/tree/comment-posting/server

polhcujo

polhcujo1#

如果我理解你正确,你想添加作者时添加文章。在这种情况下,您需要通过指定级联类型来告诉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将正确处理关联。

相关问题