Hibernate删除行和外键行ManyToOne

时间:2013-10-03 20:02:06

标签: java hibernate constraints one-to-many foreign-key-relationship

我有以下两个类,一个ReqCandAssociation可以有很多注释,它就像这样映射。我需要找出一种方法,当我删除ReqCandAssociation时,它会删除所有相关的注释。感谢

@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;

1 个答案:

答案 0 :(得分:1)

将此更改为以下内容,我正在进行双向映射。

@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

级联all将删除与此类关联的所有注释。