Hibernate克隆对象

  

如果有两个MODEL(A,A_log) ,A_log包含A的所有元素(包括A的ID),A_log唯一多的字段是自己的ID,那么在想在修改A之前的时候记录他的结果到A_log表中。可以这样写:

可以在A_log的MODEL中加一行:private A a;(set,get方法)

然后在A_log.xml中这样写:

<hibernate-mapping>
    <class name="ie.cdclab.detect.eruption.model.LabEruptionBDectctLog" table="LAB_ERUPTION_B_DECTCT_LOG" schema="LAB">
        <comment>发热伴出疹细菌检测表_log。</comment>
        <id name="idEruptionBDectctLog" type="java.lang.String">
            <column name="ID_ERUPTION_B_DECTCT_LOG" length="40" />
            <generator class="uuid" />
        </id>
                <component name="labEruptionBDetect">   --用这个把相同的元素包起来,labEruptionBDetect是原表
        <property name="idEruptionBDectct" type="java.lang.String">
            <column name="ID_ERUPTION_B_DECTCT" length="40" not-null="true">
                <comment>发热伴出疹细菌检测表ID。</comment>
            </column>
        </property>
        <property name="idSampleinfo" type="java.lang.String">
            <column name="ID_SAMPLEINFO" length="40" not-null="true">
                <comment>标本信息ID。</comment>
            </column>
        </property>
        </component>
    </class>
</hibernate-mapping>

然后在service中:

public void saveLog(LabEruptionBDetect LabEruptionBDetect) {
		// TODO Auto-generated method stub
		
		LabEruptionBDectctLog LabEruptionBDetectLog = new LabEruptionBDectctLog();
		LabEruptionBDetectLog.setLabEruptionBDetect(LabEruptionBDetect);
		dao.save(LabEruptionBDetectLog);
	}

相关推荐