简体   繁体   English

Hibernate JPA:传递给持久化的分离实体

[英]Hibernate JPA: detached entity passed to persist

The model contains: 该模型包含:

StationSecondaire class : StationSecondaire

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

and StationPrincipale class : StationPrincipale

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

And i tried to obtain an existent StationPrincipale in database by: 我试图通过以下方式在数据库中获取现有的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);

After that, I created some new StationSecondaire object and I attached them to the sp . 之后,我创建了一些新的StationSecondaire对象,并将它们附加到sp

When i tried to persist StationSecondaire object, i got that error : 当我试图坚持StationSecondaire对象时,我得到了这个错误

detached entity passed to persist: StationPrincipale

How can i fix it so that i can add an StationSecondaire object attached to an existent StationPrincipale one? 我该如何修复它以便我可以添加附加到现有StationPrincipaleStationSecondaire对象?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM