Question

The model contains:

StationSecondaire class:

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

and StationPrincipale class:

@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 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.

When i tried to persist StationSecondaire object, i got that error:

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?

Was it helpful?

Solution

persist is for new values, so use merge instead of it, because sp already exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top