title: Hibernate JPA: detached entity passed to persist
body: <p>The model contains:</p>
  
  <p><code>StationSecondaire</code> <strong>class</strong>: </p>
  
  <pre><code>@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinColumn(name = "station_principal_id", nullable = false)
  public StationPrincipale getStationPrincipale() {
      return this.stationPrincipale;
  }
  </code></pre>
  
  <p>and 
  <code>StationPrincipale</code> <strong>class</strong>:</p>
  
  <pre><code>@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, mappedBy = "stationPrincipale")
  public Set&lt;StationSecondaire&gt; getStationSecondaires() {
      return this.stationSecondaires;
  }
  </code></pre>
  
  <p>And i tried to obtain an existent <code>StationPrincipale</code>  in database by:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>After that, I created some new <code>StationSecondaire</code> object and I attached them to the <code>sp</code>.</p>
  
  <p>When i tried to persist <code>StationSecondaire</code> object, i got that <strong>error</strong>:</p>
  
  <pre><code>detached entity passed to persist: StationPrincipale
  </code></pre>
  
  <p>How can i fix it so that i can <strong>add</strong> an <code>StationSecondaire</code> object attached to an existent <code>StationPrincipale</code> one?</p>