【发布时间】:2013-12-16 06:45:22
【问题描述】:
模型包含:
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
我该如何解决它,以便我可以添加一个 StationSecondaire 对象附加到现有的 StationPrincipale 对象?
【问题讨论】:
-
能否提供更多代码?特别是在你保存实体的地方..
-
我更新了这个问题,谢谢
标签: java hibernate jpa persist