问题已解决...hibernate不支持递归级联..
解决方案 »
- 急!关于iReport换行打印问题
- 如何生成一串不相同的随机数(java)?
- Appfuse Hello World 问题
- 关于struts2使用pager-taglib的问题 请高手指教 恳求
- 北大青鸟ACCP怎么样???好不好???
- 开发WebService用Axis2还是Axis1还是JAX-WS?
- J2ee 源代碼下載網站
- 菜鸟之问:j2eesdk里面都有什么东西?
- struts学习时候tomcat的报错org.apache.struts.action.MESSAGE
- 在部署SESSION BEAN时候遇到的问题!
- 一个很困扰我关于FileUpload的问题需大家急救啊
- ssh框架,tomcat启动问题
现在问题变成了我删除孙子节点后只能接着删父节点然后才能删爷爷节点
因为级联只能级联刷新上一级的数据
我直接删爷爷节点就报脏数据异常
求解...
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;
}
}
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;
}
}
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();
}
}