如何实现Hibernate树行结构的删除

在同一张表存在所属关系的树形结构,如何实现删除某个节点同时把该节点下的所有子节点一起删除
下边是部分结构说明
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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值