2014-09-10 83 views
0

我正在使用彈簧Propagation.REQUIRED更新數據庫中的行,它第一次更新正確,但第二次拋出異常。Jpa事務更新拋出第二次更新的異常

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

我認爲@Transactional(propagation = Propagation。)是一個錯字? – Lucas 2014-09-10 11:06:50

+0

是的,它是錯字,實際值是@Transactional(propagation = Propagation.REQUIRED),將更新代碼。 – user3157090 2014-09-10 11:09:10

回答

0

在您控制器@RequestMapping註釋,你需要把通過wadRepository.updatePublisher(...)返回的實例在JsonDataWrapper<PublisherLookupItem> result

從您的代碼中,現在您將在響應中返回PublisherLookupItem的陳舊實例,因此下次嘗試合併該實例時會出現陳舊的對象異常。