问题描述
- hibernate ManyToMany
-
做了两个实体类,任务和成员,一个任务有多个成员,一个成员属于多个任务,多对多关系,结构如下:
任务类:
@Entity
@Table(name = "pms_schedule")
public class Schedule implements Serializable{
@Id
@GeneratedValue
private int seqId;//主键@ManyToMany(cascade = { CascadeType.PERSIST, javax.persistence.CascadeType.MERGE },fetch = FetchType.LAZY,mappedBy = "schedules") private Set<Person> attendPersons;//参与人 public int getSeqId() { return seqId; } public void setSeqId(int seqId) { this.seqId = seqId; @JsonIgnore public Set<Person> getAttendPersons() { return attendPersons; } public void setAttendPersons(Set<Person> attendPersons) { this.attendPersons = attendPersons; }
}
成员类:
@Entity
@Table(name = "person")
public class Person implements Serializable{
@Id
private int seqId;//主键@ManyToMany(cascade = { CascadeType.PERSIST, javax.persistence.CascadeType.MERGE },fetch = FetchType.LAZY) @JoinTable(name = "person_schedule",joinColumns = {@JoinColumn(name = "person_id")},inverseJoinColumns = {@JoinColumn(name = "schedule_id")}) private Set<Schedule> schedules; public int getSeqId() { return seqId; } public void setSeqId(int seqId) { this.seqId = seqId; } @JsonIgnore public Set<Schedule> getSchedules() { return schedules; } public void setSchedules(Set<Schedule> schedules) { this.schedules = schedules; }
}
成员表是关系维护者,在任务控制层用session.delete()方法删除任务和成员-任务关系时会报错(受外键制约不能删除)。
如果在数据库中把成员-任务表中的外键属性ON DELETE 改成 CASCADE,再使用session.delete()方法删除任务,就可以级联把任务和成员-任务表对应的数据都删除那么在hibernate里又没办法在创建表时就把ON DELETE 属性设成 CASCADE ?
本人新人一个(拿不出悬赏),请各位大神指教
时间: 2024-11-29 21:00:55