A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 年少丶 中级黑马   /  2014-3-5 17:35  /  1088 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

删除一个部门,其下属部门将全部删除。
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.         }
复制代码



评分

参与人数 1技术分 +1 收起 理由
朱神必 + 1

查看全部评分

1 个回复

倒序浏览
  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. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马