在同一张表存在所属关系的树形结构,如何实现删除某个节点同时把该节点下的所有子节点一起删除
下边是部分结构说明
CstBomMasterTest.hbm.xml
<hibernate-mapping>
<class name="test.CstBomMasterTest"
table="CST_BOM_MASTER_TEST" schema="CAPMS">
<id name="id" type="long" column="ID">
<generator class="increment" />
</id>
<property name="code" type="string">
<column name="CODE" length="20" />
</property>
<property name="name" type="string">
<column name="NAME" length="20" />
</property>
<!--
<property name="parentCode" type="string">
<column name="PARENT_CODE" length="20" />
</property>-->
<set name="childCstBomMasterTests" cascade="all" inverse="true">
<key column="parent_code" />
<one-to-many class="test.CstBomMasterTest" />
</set>
<many-to-one name="parentCstBomMasterTest" column="parent_code"
class="test.CstBomMasterTest" cascade="all" />
</class>
</hibernate-mapping>
java类文件
public class CstBomMasterTest implements java.io.Serializable {
private Long id;
private String code;
private String name;
private CstBomMasterTest parentCstBomMasterTest;
private Set childCstBomMasterTests = new HashSet();
public CstBomMasterTest(String code, String name,
CstBomMasterTest parentCstBomMasterTest, Set childCstBomMasterTests) {
this.code = code;
this.name = name;
this.parentCstBomMasterTest = parentCstBomMasterTest;
this.childCstBomMasterTests = childCstBomMasterTests;
}
public CstBomMasterTest() {
}
public CstBomMasterTest(Set childCstBomMasterTests) {
this.childCstBomMasterTests = childCstBomMasterTests;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public CstBomMasterTest getParentCstBomMasterTest() {
return this.parentCstBomMasterTest;
}
public void setParentCstBomMasterTest(
CstBomMasterTest parentCstBomMasterTest) {
this.parentCstBomMasterTest = parentCstBomMasterTest;
}
public Set getChildCstBomMasterTests() {
return this.childCstBomMasterTests;
}
public void setChildCstBomMasterTests(Set childCstBomMasterTests) {
this.childCstBomMasterTests = childCstBomMasterTests;
}
public void addChildCstBomMasterTest(CstBomMasterTest cstBomMasterTest) {
if (cstBomMasterTest == null)
throw new IllegalArgumentException(
"Can't add a null cstBomMasterTest as child.");
// Remove from old parent category
if (cstBomMasterTest.getParentCstBomMasterTest() != null)
cstBomMasterTest.getParentCstBomMasterTest()
.getChildCstBomMasterTests().remove(cstBomMasterTest);
// Set parent in child
cstBomMasterTest.setParentCstBomMasterTest(this);
// Set child in parent
this.getChildCstBomMasterTests().add(cstBomMasterTest);
}
public String toString() {
return new ToStringBuilder(this).append("id", getId()).toString();
}
}
下边是部分结构说明
CstBomMasterTest.hbm.xml
<hibernate-mapping>
<class name="test.CstBomMasterTest"
table="CST_BOM_MASTER_TEST" schema="CAPMS">
<id name="id" type="long" column="ID">
<generator class="increment" />
</id>
<property name="code" type="string">
<column name="CODE" length="20" />
</property>
<property name="name" type="string">
<column name="NAME" length="20" />
</property>
<!--
<property name="parentCode" type="string">
<column name="PARENT_CODE" length="20" />
</property>-->
<set name="childCstBomMasterTests" cascade="all" inverse="true">
<key column="parent_code" />
<one-to-many class="test.CstBomMasterTest" />
</set>
<many-to-one name="parentCstBomMasterTest" column="parent_code"
class="test.CstBomMasterTest" cascade="all" />
</class>
</hibernate-mapping>
java类文件
public class CstBomMasterTest implements java.io.Serializable {
private Long id;
private String code;
private String name;
private CstBomMasterTest parentCstBomMasterTest;
private Set childCstBomMasterTests = new HashSet();
public CstBomMasterTest(String code, String name,
CstBomMasterTest parentCstBomMasterTest, Set childCstBomMasterTests) {
this.code = code;
this.name = name;
this.parentCstBomMasterTest = parentCstBomMasterTest;
this.childCstBomMasterTests = childCstBomMasterTests;
}
public CstBomMasterTest() {
}
public CstBomMasterTest(Set childCstBomMasterTests) {
this.childCstBomMasterTests = childCstBomMasterTests;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public CstBomMasterTest getParentCstBomMasterTest() {
return this.parentCstBomMasterTest;
}
public void setParentCstBomMasterTest(
CstBomMasterTest parentCstBomMasterTest) {
this.parentCstBomMasterTest = parentCstBomMasterTest;
}
public Set getChildCstBomMasterTests() {
return this.childCstBomMasterTests;
}
public void setChildCstBomMasterTests(Set childCstBomMasterTests) {
this.childCstBomMasterTests = childCstBomMasterTests;
}
public void addChildCstBomMasterTest(CstBomMasterTest cstBomMasterTest) {
if (cstBomMasterTest == null)
throw new IllegalArgumentException(
"Can't add a null cstBomMasterTest as child.");
// Remove from old parent category
if (cstBomMasterTest.getParentCstBomMasterTest() != null)
cstBomMasterTest.getParentCstBomMasterTest()
.getChildCstBomMasterTests().remove(cstBomMasterTest);
// Set parent in child
cstBomMasterTest.setParentCstBomMasterTest(this);
// Set child in parent
this.getChildCstBomMasterTests().add(cstBomMasterTest);
}
public String toString() {
return new ToStringBuilder(this).append("id", getId()).toString();
}
}