hibernate 联合主键 merge

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
 
=======================================================================================
复合(联合)主键映射

通常将复合主键相关的属性,单独放到一个类中
 * 此类必须实现序列化接口
 * 覆写hashcode和equals方法

===========================================================================================
昨天使用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()--脱管状态时候调用,状态不变
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值