[英]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
我該如何修復它以便我可以添加附加到現有StationPrincipale
的StationSecondaire
對象?
persist
是新值,因此使用merge
而不是它,因為sp
已經存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.