hibernate 聯合主鍵 merge

來源:互聯網
上載者:User

http://topic.csdn.net/u/20090722/16/1946dd91-3900-47e9-ab85-ed7db41252de.html Object obj=session.merge(entry.getClass().getName(), entry);
entry 中的主鍵都是有值的,但是obj中的主鍵都是null,且資料庫中也是空值
一頭霧水啊。主鍵是複合主鍵, 單表,沒有關聯關係。 我用的hibernate 是3.0
現在發現規律,如果 entry是全新的,merge就會出現 主鍵被清空的問題。
如果session緩衝中,有和entry相同主鍵的對象存在,merge就正常。
難道是我對merge的理解有問題,必須是session中有同主鍵的對象存在時,才能使用merge,否則應該使用saveOrUpdate????????????????   merge跟update其實一個效果,只是名字不同罷了.
merge是jpa中的方法,大概是hibernate要跟jpa接軌,
才出的這個方法
save---persist   ======================================================================================= http://iblike.iteye.com/blog/795721 複合(聯合)主鍵映射

通常將複合主鍵相關的屬性,單獨放到一個類中
 * 此類必須實現序列化介面
 * 覆寫hashcode和equals方法 =========================================================================================== http://fei-6666.iteye.com/blog/446012 昨天使用hibernate迴圈批量儲存一個聯合主鍵類報出如下錯誤:
AbstractFlushingEventListener[line:301}: Could not synchronize database state with session
Could not execute JDBC batch update

聯合主鍵類配置如下:
<hibernate-mapping>
    <class name="com.vcmchina.credithour.model.SzGcEvaResult" table="sz_gc_eva_result" >
        <composite-id name="id" class="com.vcmchina.credithour.model.SzGcEvaResultId">
            <key-property name="stuNo" type="java.lang.String">
                <column name="stu_no" length="20" />
            </key-property>
            <key-property name="projectCode" type="java.lang.String">
                <column name="project_code" length="20" />
            </key-property>
            <key-property name="weekNo" type="java.lang.Integer">
                <column name="week_no" />
            </key-property>
            <key-property name="syCode" type="java.lang.String">
                <column name="sy_code" length="20" />
            </key-property>
            <key-property name="semSection" type="java.lang.String">
                <column name="sem_section" length="10" />
            </key-property>
            <key-property name="suNo" type="java.lang.String">
                <column name="su_no" length="20" />
            </key-property>
            <key-property name="semNo" type="java.lang.String">
                <column name="sem_no" length="20" />
            </key-property>
        </composite-id>
        <property name="secType" type="java.lang.String">
            <column name="sec_type" length="10" not-null="true" />
        </property>
        <property name="graType" type="java.lang.String">
            <column name="gra_type" length="10" not-null="true" />
        </property>
        <property name="semFlag" type="java.lang.String">
            <column name="sem_flag" length="10" not-null="true" />
        </property>
        <property name="evaScore" type="java.lang.Integer">
            <column name="eva_score" not-null="true" />
        </property>
        <property name="recordData" type="java.lang.String">
            <column name="record_data" length="30" not-null="true" />
        </property>
        <property name="empName" type="java.lang.String">
            <column name="emp_name" length="50" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

////////////////////////////////////////////////

開始採用的是
public void addSzGcEvaResult(SzGcEvaResult evaResult) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(evaResult);
}

後來改用merge方法就沒有問題了:
public void addSzGcEvaResult(SzGcEvaResult evaResult) {
// TODO Auto-generated method stub
this.getHibernateTemplate().merge(evaResult);
}

我感覺是對象狀態的問題,因為我給對象執行個體化了聯合主鍵,hibernate 認為它不再是瞬態,而是託管狀態。
save() --瞬態狀態調用,狀態變為持久(POJO)
merge()--脫管狀態時候調用,狀態不變

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.