2015-05-06 50 views
0

我有以下映射在我的豆2NonUniqueObjectException:用同樣的標識值不同的對象已經與會議

父bean

@OneToMany(fetch = FetchType.LAZY, mappedBy = "application") 
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE}) 
@JsonIgnore 
private Set<ApplicationBuildVO> applicationBuilds = new HashSet<ApplicationBuildVO>(0); 

子bean

相關
@ManyToOne(fetch = FetchType.LAZY) 
@Cascade({CascadeType.SAVE_UPDATE}) 
@JoinColumn(name = "APPLICATION_ID", nullable = false) 
@JsonIgnore 
private ApplicationVO application; 

在我的服務方法 - 我執行查找應用程序bea ñ 創建一個子應用程序豆 - 其中有原始申請的參考

然後 - 當我嘗試在同一服務方法我收到以下錯誤,更新的父bean的值

Caused by: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.domain.dao.ApplicationVO#1] 
    at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:617) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:301) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:244) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 

我的服務代碼(精簡)如下

public boolean performBuild(Integer applicationId) { 
     ApplicationVO applicationVO = applicationServices.findAnyApplicationId(applicationId); 

     ApplicationBuildVO androidApplicationBuildVO = new ApplicationBuildVO(); 
     androidApplicationBuildVO.setIsAndroid(true); 
     androidApplicationBuildVO.setIsSuccess(appBuildSuccessful); 
     androidApplicationBuildVO.setApplication(applicationVO); 
     androidApplicationBuildVO.setSoftwareVersion(androidSoftwareVersionVO); 
     androidApplicationBuildVO.setS3BucketName(mobileAppsBucketName); 
     androidApplicationBuildVO.setS3ArtifactKey(androidArtifact); 

     Integer applicationBuildId = applicationBuildServices.saveApplicationBuild(androidApplicationBuildVO); 

     applicationVO.setIsCurrentlyBeingBuilt(false); 
     boolean updateApplicationResult = applicationServices.updateApplication(applicationVO);   
} 

任何想法我可以做什麼來解決這個問題?

+1

你能發佈你的更新代碼嗎? –

+0

當然 - 只是更新了帖子 –

+1

你的「@ Id」是如何分配的?我假設有某種'@ GeneratedValue' ... – Powerlord

回答

1

我認爲你的問題是你要保存androidApplicationBuildVO孩子第一次造成的事實之後,你救母applicationVO你可以在孩子加入到父母和保存這樣的家長:

boolean performBuild(Integer applicationId) { 
    ApplicationVO applicationVO = applicationServices.findAnyApplicationId(applicationId); 

    ApplicationBuildVO androidApplicationBuildVO = new ApplicationBuildVO(); 
    androidApplicationBuildVO.setIsAndroid(true); 
    androidApplicationBuildVO.setIsSuccess(appBuildSuccessful); 
    androidApplicationBuildVO.setApplication(applicationVO); 
    androidApplicationBuildVO.setSoftwareVersion(androidSoftwareVersionVO); 
    androidApplicationBuildVO.setS3BucketName(mobileAppsBucketName); 
    androidApplicationBuildVO.setS3ArtifactKey(androidArtifact); 

    applicationVO.addBuild(androidApplicationBuildVO); 

    applicationVO.setIsCurrentlyBeingBuilt(false); 
    boolean updateApplicationResult = applicationServices.updateApplication(applicationVO);   
    } 

而且如果不存在,在applicationVO

public void addBuild(ApplicationBuildVO buildVo) { 
    applicationBuilds.add(buildVo); 
} 

在ApplicationBuildVO謹防equalshashcode實現不立足於我,你應該添加此方法d添加未保存的元素時沒有看到applicationBuilds設置。

+0

完美的解決了這個問題 - 謝謝主人翁 –

+1

完美的你是受歡迎的 –

相關問題