黑马程序员技术交流社区

标题: 求帮忙,关于hibernate级联删除的 [打印本页]

作者: 年少丶    时间: 2014-3-5 17:35
标题: 求帮忙,关于hibernate级联删除的
删除一个部门,其下属部门将全部删除。
casCade应该就是这么写的,可是不对呀
  1. @Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN,CascadeType.ALL})
复制代码

这是dao层的
  1. @Override
  2.         public void delete(int id) {
  3.                 Object obj = findById(id);
  4.                 if(obj != null){
  5.                         getSession().delete(obj);
  6.                 }
  7.         }
复制代码
  1. public Department findById(int id) {
  2.                 if(id == 0){
  3.                         return null;
  4.                 }else{
  5.                         return (Department) getSession().get(Department.class, id);
  6.                 }
  7.         }
复制代码




作者: 年少丶    时间: 2014-3-5 17:36
  1. package cn.my.oa.model;

  2. import java.util.HashSet;
  3. import java.util.Set;

  4. import javax.persistence.Entity;
  5. import javax.persistence.FetchType;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.Id;
  8. import javax.persistence.ManyToOne;
  9. import javax.persistence.OneToMany;

  10. import org.hibernate.annotations.Cascade;
  11. import org.hibernate.annotations.CascadeType;
  12. import org.hibernate.annotations.Fetch;
  13. import org.hibernate.annotations.FetchMode;
  14. /*
  15. * 部门
  16. * */
  17. @Entity
  18. public class Department {
  19.         private  int id;//部门id
  20.         private String name;//部门名称
  21.         private String description;//部门描述
  22.         private Set<User> users = new HashSet<User>();//用户
  23.         private Department parent;//上级部门
  24.         private Set<Department> children = new HashSet<Department>();//子部门
  25.        
  26.         @OneToMany
  27.         public Set<User> getUsers() {
  28.                 return users;
  29.         }
  30.         public void setUsers(Set<User> users) {
  31.                 this.users = users;
  32.         }
  33.         @ManyToOne
  34.         public Department getParent() {
  35.                 return parent;
  36.         }
  37.         public void setParent(Department parent) {
  38.                 this.parent = parent;
  39.         }
  40.        
  41.        
  42.         @Id@GeneratedValue
  43.        
  44.         public int getId() {
  45.                 return id;
  46.         }
  47.         @OneToMany(fetch = FetchType.LAZY)
  48.         @Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN,CascadeType.ALL})  
  49.         public Set<Department> getChildren() {
  50.                 return children;
  51.         }
  52.         public void setChildren(Set<Department> children) {
  53.                 this.children = children;
  54.         }
  55.         public void setId(int id) {
  56.                 this.id = id;
  57.         }
  58.         public String getName() {
  59.                 return name;
  60.         }
  61.         public void setName(String name) {
  62.                 this.name = name;
  63.         }
  64.         public String getDescription() {
  65.                 return description;
  66.         }
  67.         public void setDescription(String description) {
  68.                 this.description = description;
  69.         }
  70.        
  71. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2