hibernate 注解 manytomany 级联删除问题
定义了两个类,Account和Department。是多对多关系,类定义如下;
@Entity
@Table(name="ACCOUNTS")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
publicclassAccount{
privateLongid;
privateStringloginName;
privateList<Department>departments;
...
@Override
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_STORE")
@SequenceGenerator(name="SEQ_STORE",sequencename="ACCOUNTS_ID",allocationSize=2)
@Column(length=12)
publicLonggetId(){
returnid;
}
publicvoidsetId(Longid){
this.id=id;
}
@ManyToMany(fetch=FetchType.LAZY)
@OrderBy("prioritydesc")
@JoinTable(name="ACCOUNT_DEPARTMENT_ACCOUNT",joinColumns={@JoinColumn(name="account_id",nullable=false,updatable=false)},inverseJoinColumns={@JoinColumn(name="department_id",nullable=false,updatable=false)})
@Cascade({CascadeType.SAVE_UPDATE})
publicList<Department>getDepartments(){
returndepartments;
}
publicvoidsetDepartments(List<Department>departments){
this.departments=departments;
}
}
@Entity
@Table(name="ACCOUNT_DEPARTMENTS")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
publicclassDepartment{
privateLongid;
privateList<Department>children;
privateList<Account>accounts;
@Override
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_STORE")
@SequenceGenerator(name="SEQ_STORE",sequencename="ACCOUNT_DEPARTMENTS_ID",allocationSize=2)
@Column(length=12,name="ID")
publicLonggetId(){
returnid;
}
publicvoidsetId(Longid){
this.id=id;
}
@OneToMany(fetch=FetchType.LAZY)
@OrderBy("prioritydesc")
@Cascade(CascadeType.ALL)
@JoinColumn(name="parentId")
publicList<Department>getChildren(){
returnchildren;
}
publicvoidsetChildren(List<Department>children){
this.children=children;
}
@ManyToMany(fetch=FetchType.LAZY)
@OrderBy("prioritydesc")
@JoinTable(name="ACCOUNT_DEPARTMENT_ACCOUNT",joinColumns={@JoinColumn(name="department_id",nullable=false,updatable=false)},inverseJoinColumns={@JoinColumn(name="account_id",nullable=false,updatable=false)})
@Cascade({CascadeType.SAVE_UPDATE})
publicList<Account>getAccounts(){
returnaccounts;
}
publicvoidsetAccounts(List<Account>accounts){
this.accounts=accounts;
}
}
自动在数据库中生成三个表ACCOUNTS,ACCOUNT_DEPARTMENTS,和中间表ACCOUNT_DEPARTMENT_ACCOUNT。
现在我删除部门表(ACCOUNT_DEPARTMENTS)的一个记录时,(假设id=3删除),它会把中间表ACCOUNT_DEPARTMENT_ACCOUNT中的department_id=3的记录删除。
还会把ACCOUNT_DEPARTMENT_ACCOUNT中department_id=3对应的人员表记录id,例如是account_id=4,account_id=5,把ACCOUNTS中的id为4,和5的记录也会删掉。
同时还会把ACCOUNT_DEPARTMENTS中id=3,和子部门=3的记录也删掉。
我是想只删掉中间表中department_id=3,和ACCOUNT_DEPARTMENTS中id=3的记录。
请问这样该怎么实现呢?