【问题标题】:Hibernate JPA: detached entity passed to persistHibernate JPA:分离实体传递给持久化
【发布时间】: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


【解决方案1】:

persist 用于新值,因此请使用merge 代替它,因为sp 已经存在。

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2013-06-26
    • 2018-01-05
    • 2015-12-26
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多