在没有实际编程之前,看过一些关于hibernate级联保存和级联删除的内容,记得当时我看明白了,后来实践编码的时候才发现原来还是理解的不透彻。今天我做了一个有关的编码,在这里分享给大家,希望对大家有用
Task.xml(这是“一”的一方)
<hibernate-mapping>
<class name="com.hibernate.bean.Task" table="TASK" >
<id name="id" type="java.lang.Integer">
<column name="ID" precision="9" scale="0" />
<generator class="sequence" >
<param name="sequence">TASK_SEQ</param>
</generator>
</id>
<many-to-one name="depSec" class="com.hibernate.bean.DepSec" fetch="select" lazy="false">
<column name="TARGET" precision="9" scale="0" not-null="true" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="limitTime" type="java.util.Date">
<column name="LIMIT_TIME" length="7" not-null="true" />
</property>
<property name="isFeedback" type="java.lang.String">
<column name="IS_FEEDBACK" length="2" />
</property>
<property name="accessory" type="java.lang.String">
<column name="ACCESSORY" length="100" />
</property>
<property name="content" type="java.lang.String">
<column name="CONTENT" length="500" />
</property>
<property name="year" type="java.lang.Integer">
<column name="YEAR" precision="9" scale="0" />
</property>
<set name="taskDetails" inverse="true" cascade="delete">
<key>
<column name="TASK_ID" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="com.hibernate.bean.TaskDetail" />
</set>
</class>
</hibernate-mapping>
TaskDetail.xml(这是“多”的一方)
<hibernate-mapping>
<class name="com.hibernate.bean.TaskDetail" table="TASK_DETAIL" >
<id name="id" type="java.lang.Integer">
<column name="ID" precision="9" scale="0" />
<generator class="sequence" >
<param name="sequence">TASK_DETAIL_SEQ</param>
</generator>
</id>
<many-to-one name="company" class="com.hibernate.bean.Company" fetch="join" lazy="false">
<column name="COMPANY_ID" precision="9" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="task" class="com.hibernate.bean.Task" fetch="join" lazy="false" cascade="save-update">
<column name="TASK_ID" precision="9" scale="0" not-null="true" />
</many-to-one>
<property name="completeTime" type="java.util.Date">
<column name="COMPLETE_TIME" length="7" />
</property>
<property name="lookTime" type="java.util.Date">
<column name="LOOK_TIME" length="7" />
</property>
<property name="auditTime" type="java.util.Date">
<column name="AUDIT_TIME" length="7" />
</property>
<property name="status" type="java.lang.String">
<column name="STATUS" length="20" />
</property>
<property name="completeReport" type="java.lang.String">
<column name="COMPLETE_REPORT" length="100" />
</property>
<property name="completeCondition" type="java.lang.String">
<column name="COMPLETE_CONDITION" length="200" />
</property>
<property name="disqualification" type="java.lang.Integer">
<column name="DISQUALIFICATION" precision="9" scale="0" />
</property>
</class>
</hibernate-mapping>
测试类
public void testInsertTask() {
Date limitTime = new Date(2011-1900, 9, 5);
Task task = new Task();
task.setYear(2011);
task.setContent("任务的详细信息");
task.setIsFeedback("否");
task.setLimitTime(limitTime);
DepSec depSec = actionService.listTarget().get(2);
task.setDepSec(depSec);
task.setName("任务1");
TaskDetail detail = new TaskDetail();
detail.setTask(task);
detail.setStatus("未查看");
List<Company> list = this.companyService.listAllCompanyExceptJX();
for (Company company : list) {
detail.setCompany(company);
this.taskDetailService.insert(detail);
}
}
public void deleteTask(int id){
Task task = this.taskService.findById(id);
this.taskService.delete(id);
}
总结:要想达到级联保存的效果,需要在“多”的一方配置cascade=“save-update”;“一”的一方配置inverse=“true”
要想达到级联删除的结果,需要在“多”的一方配置cascade=“delete”