问题已解决...hibernate不支持递归级联..

解决方案 »

  1.   

    貌似改成CascadeType.REMOVE后能递归删除了 
    现在问题变成了我删除孙子节点后只能接着删父节点然后才能删爷爷节点 
    因为级联只能级联刷新上一级的数据  
    我直接删爷爷节点就报脏数据异常  
     
     
    求解...
      

  2.   

    我单独写了个最简单的递归映射的DEMO
    import java.util.List;
    import java.util.Set;import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;@Entity
    public class Test  {
    private Long id;
    private Test parent;//父部门
        private Set<Test> children; //通过Set将下级加入该实体 子部门
       
        private String name;
        @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long getId() {
    return id;
    } public void setId(Long id) {
    this.id = id;
    }
    @ManyToOne
    public Test getParent() {
    return parent;
    } public void setParent(Test parent) {
    this.parent = parent;
    } @OneToMany(mappedBy ="parent",fetch=FetchType.EAGER,cascade=CascadeType.ALL )
    public Set<Test> getChildren() {
    return children;
    } public void setChildren(Set<Test> children) {
    this.children = children;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }
    }



      

  3.   

    import java.util.List;
    import java.util.Set;import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;@Entity
    public class Test  {
    private Long id;
    private Test parent;//父部门
        private Set<Test> children; //通过Set将下级加入该实体 子部门
       
        private String name;
        @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long getId() {
    return id;
    } public void setId(Long id) {
    this.id = id;
    }
    @ManyToOne
    public Test getParent() {
    return parent;
    } public void setParent(Test parent) {
    this.parent = parent;
    } @OneToMany(mappedBy ="parent",fetch=FetchType.EAGER,cascade=CascadeType.ALL )
    public Set<Test> getChildren() {
    return children;
    } public void setChildren(Set<Test> children) {
    this.children = children;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }
    }




      

  4.   

    报错org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [Test#2]
      

  5.   

    删除代码贴错了。
    import java.util.HashSet;import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;public class Test2 {

    public static void main(String[] a) {

    //PropertyConfigurator.configure(Test.class.getClassLoader().getResource("log4j.property"));

    Configuration config = new AnnotationConfiguration().configure();
    SessionFactory sf = config.buildSessionFactory();
    SchemaExport export = new SchemaExport(config);
    export.create(true, true); Session s = sf.openSession();
    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = new Test();
    Test t4 = new Test();
    HashSet<Test> hs1 = new HashSet<Test>();
    HashSet<Test> hs2 = new HashSet<Test>();
    HashSet<Test> hs3 = new HashSet<Test>();
    hs1.add(t1);
    hs2.add(t2);
    hs3.add(t3);
    t4.setChildren(hs3);
    t3.setParent(t4);
    t3.setChildren(hs2);
    t2.setParent(t3);
    t2.setChildren(hs1);
    t1.setParent(t2);
    Transaction t = s.beginTransaction();
    s.save(t4);
    t.commit();
    t.begin();
    s.delete(t3);
    t.commit();
    t.begin();
    s.delete(t4);
    t.commit();
    s.close();


    }
    }