合并时Hibernate StaleDataException

时间:2013-04-05 09:23:39

标签: java spring hibernate

我有2个网络服务API。在一个api中,我获取一个实体并作为SRO返回。 我是另一个api,我收到一个SRO,将其转换为实体并使用

sessionFactory.getCurrentSession().merge(code);

但是,我得到以下异常。请帮我找一下原因。

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [PartnerPromoCode] with identifier [254874]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [PartnerPromoCode#254874]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:683)
at org.springframework.orm.hibernate3.HibernateExceptionTranslator.convertHibernateAccessException(HibernateExceptionTranslator.java:89)
at org.springframework.orm.hibernate3.HibernateExceptionTranslator.translateExceptionIfPossible(HibernateExceptionTranslator.java:68)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:199)
at sun.proxy.$Proxy48.updatePartnerPromoCode(Unknown Source)
at abc.updatePartnerPromoCode(abc.java:567)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:199)
at sun.proxy.$Proxy54.updatePartnerPromoCode(Unknown Source)
at abc.updatePartnerPromoCode(ServiceDealWebServiceController.java:272)
at xyz$$FastClassByCGLIB$$c65431c6.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)

修改

这是实体 @实体 @Table(name =“partner_promo_code”,catalog =“snapdeal”) @NamedQueries({         @NamedQuery(name = QueryNames.GET_UNUSED_PARTNER_PROMO_CODE,query =“selectP from PartnerPromoCode p,其中p.status = 0,p.partnerCode =:partnerCode和p.catalogId =:catalogId order by p.id”),         @NamedQuery(name = QueryNames.GET_PARTNER_PROMO_CODE_BY_PROMO,query =“来自PartnerPromoCode,其中partnerCode =:partnerCode和promoCode =:promoCode”)}) 公共类PartnerPromoCode实现了java.io.Serializable {

public static final Boolean STATUS_USED      = true;
public static final Boolean STATUS_NOT_USED  = false;
/**
 * 
 */
private static final long   serialVersionUID = 7786688992136091701L;
private Integer             id;
private String              partnerCode;
private String              promoCode;
private Boolean             status;
private Date                expiry;
private Date                created;
private Date                updated;

public PartnerPromoCode() {
}

public PartnerPromoCode(String partnerCode, String promoCode) {
    this.partnerCode = partnerCode;
    this.promoCode = promoCode;
    this.status = STATUS_NOT_USED;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@Column(name = "partner_code")
public String getPartnerCode() {
    return partnerCode;
}

public void setPartnerCode(String partnerCode) {
    this.partnerCode = partnerCode;
}

@Column(name = "promo_code")
public String getPromoCode() {
    return promoCode;
}

public void setPromoCode(String promoCode) {
    this.promoCode = promoCode;
}

@Column(name = "status")
public Boolean getStatus() {
    return status;
}

public void setStatus(Boolean status) {
    this.status = status;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "expiry", nullable = false, length = 19)
public Date getExpiry() {
    return expiry;
}

public void setExpiry(Date expiry) {
    this.expiry = expiry;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", nullable = false, length = 19)
public Date getCreated() {
    return created;
}

public void setCreated(Date created) {
    this.created = created;
}

@Version
@Column(name = "updated", nullable = false, length = 19)
public Date getUpdated() {
    return updated;
}

public void setUpdated(Date updated) {
    this.updated = updated;
}

}

我通过id获取实体并将其转换为SRO ,,,通过线路传递...然后我收到sro,创建一个新的实体对象并设置id和其他所有字段,设置将字段更新为当前时间,然后尝试通过sessionFactory.getSession().merge(obj)

合并实体

1 个答案:

答案 0 :(得分:0)

因为用@Version注释的字段的值与存储在数据库中的值不匹配(或者因为数据库中的行不再存在)。