簡體   English   中英

Hibernate JPA:傳遞給持久化的分離實體

[英]Hibernate JPA: detached entity passed to persist

該模型包含:

StationSecondaire

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "station_principal_id", nullable = false)
public StationPrincipale getStationPrincipale() {
    return this.stationPrincipale;
}

StationPrincipale

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, mappedBy = "stationPrincipale")
public Set<StationSecondaire> getStationSecondaires() {
    return this.stationSecondaires;
}

我試圖通過以下方式在數據庫中獲取現有的StationPrincipale

StationPrincipale sp = spDAO.findStationByName("Some name");
//Some staff
StationSecondaire ssNew = new StationSecondaire(0, ((Station) obj).getValue().toString(), null,((Station) obj).getId());
ssNew.setStationPrincipale(sp); 

//staff
ssDAO.persist(ssNew);

之后,我創建了一些新的StationSecondaire對象,並將它們附加到sp

當我試圖堅持StationSecondaire對象時,我得到了這個錯誤

detached entity passed to persist: StationPrincipale

我該如何修復它以便我可以添加附加到現有StationPrincipaleStationSecondaire對象?

persist是新值,因此使用merge而不是它,因為sp已經存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM