Hibernate删除行和外键行ManyToOne [英] Hibernate delete row and foreign key row ManyToOne
问题描述
我有以下两个类,一个ReqCandAssociation可以有很多评论,它像这样映射。我需要找出一种方式,当我删除一个ReqCandAssociation它删除所有相关的评论。感谢
I have the following two classes, one ReqCandAssociation can have many Comments and it is mapped like so. I need to figure out a way that when I delete a ReqCandAssociation it deletes all of its associated comments. Thanks
@Entity
@Table(name = "candidate_jobReq")
public class ReqCandAssociation implements Serializable {
@Id
private Integer candidateId;
@Id
private Integer jobId;
@Column(name = "reqStatus")
private String reqStatus;
@ManyToOne
@PrimaryKeyJoinColumn(name="candidateId", referencedColumnName="id")
private Candidate candidate;
@ManyToOne
@PrimaryKeyJoinColumn(name="jobId", referencedColumnName="id")
private JobReq jobReq;
public ReqCandAssociation(){
}
第二类
@Entity
@Table(name="comment")
public class Comment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="commentText")
private String commentText;
@Column(name="commentDate")
private Date commentDate;
@ManyToOne
@PrimaryKeyJoinColumn(name="reqCandAssociationId", referencedColumnName="id")
private ReqCandAssociation reqCandAssociation;
@ManyToOne
@PrimaryKeyJoinColumn(name="userId", referencedColumnName="id")
private User user;
推荐答案
将此更改为以下内容双向映射。
Change this to the following, i'm making it bidirectional mapping.
@Entity
@Table(name = "candidate_jobReq")
public class ReqCandAssociation implements Serializable {
@Id
private Integer candidateId;
@Id
private Integer jobId;
@Column(name = "reqStatus")
private String reqStatus;
@OneToMany(cascade = { CascadeType.ALL }) //this is added here.
@JoinColumn(name ="reqCandAssociationId")
private Set<Comment> comments;
-----
阅读级联选项。所有级联类型都是| none | save-update | delete | all-delete-orphan | delete-orphan
Readup more on the cascade options. All cascade types are all|none|save-update|delete|all-delete-orphan|delete-orphan
级联全部将删除与此类相关的所有注释。
The cascade all will delete all the comments associated to this class.
这篇关于Hibernate删除行和外键行ManyToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!