黑马程序员技术交流社区
标题:
求帮忙,关于hibernate级联删除的
[打印本页]
作者:
年少丶
时间:
2014-3-5 17:35
标题:
求帮忙,关于hibernate级联删除的
删除一个部门,其下属部门将全部删除。
casCade应该就是这么写的,可是不对呀
@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN,CascadeType.ALL})
复制代码
这是dao层的
@Override
public void delete(int id) {
Object obj = findById(id);
if(obj != null){
getSession().delete(obj);
}
}
复制代码
public Department findById(int id) {
if(id == 0){
return null;
}else{
return (Department) getSession().get(Department.class, id);
}
}
复制代码
作者:
年少丶
时间:
2014-3-5 17:36
package cn.my.oa.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
/*
* 部门
* */
@Entity
public class Department {
private int id;//部门id
private String name;//部门名称
private String description;//部门描述
private Set<User> users = new HashSet<User>();//用户
private Department parent;//上级部门
private Set<Department> children = new HashSet<Department>();//子部门
@OneToMany
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@ManyToOne
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
@Id@GeneratedValue
public int getId() {
return id;
}
@OneToMany(fetch = FetchType.LAZY)
@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN,CascadeType.ALL})
public Set<Department> getChildren() {
return children;
}
public void setChildren(Set<Department> children) {
this.children = children;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2