1. 程式人生 > >Hibernate級聯儲存

Hibernate級聯儲存

   在使用持久化框架Hibernate的時候,有時候會用到級聯儲存: 儲存主表記錄的同時將子表記錄一併儲存的方法通常叫級聯儲存

 java程式碼參考

//新增記錄到基表,同時新增記錄到歷史表及聯儲存
				//基物件封裝
				agentFile = new TAgentFile();
				agentFile.setAgentCode(agentNO);
				agentFile.setName(fileName);
				agentFile.setStatus(baseStatus);
				//歷史表物件封裝
				Long hisStatus = null;          //歷史子表狀態
				HashSet<TAgentFileHistory> agentFileHistorys = new HashSet<TAgentFileHistory>();
				TAgentFileHistory  agentFileHistory = new TAgentFileHistory();
				if("add".equals(fileStatus)){
					hisStatus = 1L;
					agentFileHistory.setOldSize(null);
					agentFileHistory.setOldTime(null);
					agentFileHistory.setNewSize(Long.valueOf(newSize));
					agentFileHistory.setNewTime(DateUtils.parseDate(newTime, "yyyyMMddhhmmss"));
				}else if("update".equals(fileStatus)){
					hisStatus = 2L;
					agentFileHistory.setOldSize(Long.valueOf(oldSize));
					agentFileHistory.setOldTime(DateUtils.parseDate(oldTime, "yyyyMMddhhmmss"));
					agentFileHistory.setNewSize(Long.valueOf(newSize));
					agentFileHistory.setNewTime(DateUtils.parseDate(newTime, "yyyyMMddhhmmss"));
				}else {
					hisStatus = 3L;
					agentFileHistory.setOldSize(Long.valueOf(oldSize));
					agentFileHistory.setOldTime(DateUtils.parseDate(oldTime, "yyyyMMddhhmmss"));
					agentFileHistory.setNewSize(null);
					agentFileHistory.setNewTime(null);
				}
				agentFileHistory.setStatus(hisStatus);
				agentFileHistory.setCreatedTime(new Date());
				agentFileHistory.setTAgentFile(agentFile);
				
				agentFileHistorys.add(agentFileHistory);
				
				agentFile.setTAgentFileHistories(agentFileHistorys);
				hibernateDao.insert(agentFile);
XML程式碼

hibernate配置檔案 TAgentFile.hbm.xml


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.cslc.telbetMntl.common.entity.TAgentFile" table="T_AGENT_FILE" schema="MONITOR">
        <comment>前端代理商的檔案</comment>
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence">   
    			<param name="sequence">q_global_id</param>   
    		</generator>
        </id>
        <property name="agentCode" type="java.lang.String">
            <column name="AGENT_CODE" length="32" not-null="true">
                <comment>代理商編號,ref t_dd_branch.code</comment>
            </column>
        </property>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="1024" not-null="true">
                <comment>檔名</comment>
            </column>
        </property>
        <property name="status" type="java.lang.Long">
            <column name="STATUS" precision="1" scale="0">
                <comment>狀態 1正常 0已刪除</comment>
            </column>
        </property>
        <set name="TAgentFileHistories" inverse="true" cascade="all">
            <key>
                <column name="FILE_ID" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.cslc.telbetMntl.common.entity.TAgentFileHistory" />
        </set>
    </class>
</hibernate-mapping>

關鍵程式碼cascade="all"  即是配置了級聯儲存的屬性