我有一个令人困惑的问题,我还没弄清楚如何解决。如果你能提出我如何解决问题的建议,我将不胜感激。
所以我有以下实体关系模型here。
User.class的映射是:
@Entity
@Table(name = "CRM_USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private Long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "BIRTHDATE")
private Date birthDate;
@Column(name = "EMAIL")
private String email;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private UserAdditionalInfo additionalInfo;
@ManyToOne
@JoinColumn(name = "TEAM_FK")
private Team team;
@ManyToOne
@JoinColumn(name = "JOB_FK")
private Job job;
@ManyToOne
@JoinColumn(name = "ORGANIZATION_FK", nullable = false)
private Organization organization;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Security security;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "INFO_FILE_FK")
private InfoFile profilePicture;
@ManyToOne
@JoinColumn(name = "COUNTRY_FK")
private Country country;
// Getters and Setters
}
Comment.class的映射是:
@Entity
@Table(name = "CRM_COMMENT")
public class Comment implements Serializable {
private static final long serialVersionUID = -104145851368148154L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "COMMENT_ID")
private Long id;
@ManyToOne
@JoinColumn(name = "ARTICLE_ID")
private Article article;
@ManyToOne
@JoinColumn(name = "USER_FK", nullable = false)
private User createdUser;
@Column(nullable = false)
private String comment;
@Column(name = "CREATION_DATE", nullable = false)
private Date creationDate;
@Column(name = "MODIFICATION_DATE")
private Date modificationDate;
@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true)
@JoinTable(name = "CRM_COMMENT_LIKE",
joinColumns = {@JoinColumn(name = "COMMENT_ID")},
inverseJoinColumns = {@JoinColumn(name = "USER_ID")})
private Set<User> fans = new LinkedHashSet<>();
// Getters and Setters
}
Article.class的映射是:
@Entity
@Table(name = "CRM_ARTICLE")
public class Article implements Serializable {
// other properties
@OneToMany(mappedBy = "article", cascade = {CascadeType.ALL}, orphanRemoval = true)
@OrderBy("id DESC")
private Set<Comment> comments = new LinkedHashSet<>();
// Getters and Setters
}
问题与Comment和User - CRM_COMMENT_LIKE之间的ManyToMany关系有关。
实际上,当我在评论中添加一些新的'粉丝'时,没有问题。
@Override
public boolean giveAnLikeToComment(Long commentId, User fan) {
Comment comment = commentDao.get(commentId);
if (Objects.isNull(comment)|| BooleanUtils.isTrue(comment.getFans().contains(fan))) {
return false;
}
comment.getFans().add(fan);
commentDao.update(comment);
return true;
}
当我尝试删除一些评论时会出现问题,评论中至少有一个'喜欢'/'粉丝'。
@Override
public boolean deleteCommentById(final Long commentId) {
Comment comment = commentDao.get(commentId);
if (Objects.nonNull(comment)) {
Article article = comment.getArticle();
article.getComments().remove(comment);
comment.setFans(null); // This line fix the problem
articleDao.update(article);
return true;
}
return false;
}
所以在这种情况下,我管理文章(评论的父母)和评论本身之间的关系。这很容易,因为它们之间的连接是双向的。但那些粉丝呢?我无法删除Comment和CRM_COMMENT_LIKE关系之间的连接,因为用户不知道CRM_COMMENT_LIKE或评论。我想删除评论,删除CRM_COMMENT_LIKE中创建的所有关系。但是我阻止了,因为Hibernate会抛出一个异常,上面写着:
删除的对象将通过级联重新保存(从关联中删除已删除的对象): [crm.alltogether.core.admin.model.User#1];嵌套异常是 org.hibernate.ObjectDeletedException:将重新保存已删除的对象 通过级联(从关联中删除已删除的对象): [crm.alltogether.core.admin.model.User#1]
这是我的问题,所以如果您有任何建议,我会很高兴看到它:)
最诚挚的问候,
答案 0 :(得分:0)
orphanRemoval=true
和Article
之间需要Comment
级联。然后你会这样做。
if (Objects.nonNull(comment)) {
Article a = comment.getArticle();
a.getComments().remove(comment);
articleDao.saveOrUpdate(a);
return true;
}
这会照顾删除孤立评论,因为你已经在fans
上有一个级联,它也会删除该关联。我建议您在orphanRemoval=true
上使用级联fans
。