我有 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)
编辑
这是实体 @Entity @Table(name = "partner_promo_code", catalog = "snapdeal") @NamedQueries({ @NamedQuery(name = QueryNames.GET_UNUSED_PARTNER_PROMO_CODE, query = "select p from PartnerPromoCode p where p.status=0 and p .partnerCode = :partnerCode 和 p.catalogId = :catalogId order by p.id"), @NamedQuery(name = QueryNames.GET_PARTNER_PROMO_CODE_BY_PROMO, query = "from PartnerPromoCode where partnerCode = :partnerCode and 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)