我正在使用spring Propagation.REQUIRED
更新数据库中的行,它第一次正常更新,但第二次抛出异常。
03:44:58,206 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/wad].[springMain]] (http--127.0.0.1-8080-1) Servlet.service() for servle
t springMain threw exception: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect
): [com.guthyrenker.wad.core.model.lookup.PublisherLookupItem#4025]
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:492) [hibernate-core-4.0.1.Final.jar:4.0.1.Fin
al]
代码:
@RequestMapping(value = "/admin/updatePublisher.htm", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
JsonDataWrapper<PublisherLookupItem> updatePublisher(@RequestBody("PublisherLookupItem") publisherLookupItem, HttpServletRequest request) throws Exception {
wadRepository.updatePublisher(publisherLookupItem);
JsonDataWrapper<PublisherLookupItem> result = new JsonDataWrapper<PublisherLookupItem>(publishertList);
return result;
}
package com.wad.service
public interface WadRepository
{
@Transactional(propagation = Propagation.REQUIRED)
public PublisherLookupItem updatePublisher(PublisherLookupItem publisherLookupItem);
}
package com.wad.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.wad.core.dao.WadDAO;
import com.wad.core.model.lookup.PublisherLookupItem;
import com.wad.core.service.ServiceException;
import com.wad.core.service.WadRepository;
@Repository
public class WadRepositoryImpl implements WadRepository
{
@Autowired
private WadDAO wadDAO;
@Override
public PublisherLookupItem updatePublisher(PublisherLookupItem publisherLookupItem) {
return wadDAO.savePublishers(publisherLookupItem);
}
}
@Repository
public class WadDAO
{
public PublisherLookupItem savePublishers(PublisherLookupItem publisherLookupItem) {
publisherLookupItem = em.merge(publisherLookupItem);
return publisherLookupItem;
}
}
答案 0 :(得分:0)
在使用@RequestMapping
注释的控制器中,您需要将wadRepository.updatePublisher(...)
返回的实例放在JsonDataWrapper<PublisherLookupItem> result
中。
从您的代码开始,您现在将在响应中返回PublisherLookupItem
的陈旧实例,因此下次尝试合并该实例时会出现过时的对象异常。