title: JPA - detached entity passed to persist Many to Many
body: <p>I have a problem because I use an intermediate table to save the IDs of other 2 tables (playlist and Midia for MidiaPlaylist). For a many-to-one / one-to-many relationship.</p>
  
  <p>As I'm using MapID with extra column, I create two many-to-one relationships. I use the @EmbeddedId notation to create the relation of the IDs. The mapping type does not resolve with type "MERGE".</p>
  
  <p>table Midia</p>
  
  <pre><code>id_midia
  --------
  </code></pre>
  
  <p>table MidiaPlaylist</p>
  
  <pre><code>id_playlist
  id_midia
  nr_order
  </code></pre>
  
  <p>table Playlist</p>
  
  <pre><code>id_playlist
  -------
  </code></pre>
  
  <p><strong>My PlaylistController</strong></p>
  
  <pre><code>instance.setDtCriacao(new Date(System.currentTimeMillis()));
  instance.setQtMidiaTransmissao(dualListMidias.getTarget().size());
  instance = playlistBean.salvarPlaylist(referenceValue);
  
  int i = 1;
  for(Midia midias : dualListMidias.getTarget()){             
      MidiaPlaylist midiaPlaylist = new MidiaPlaylist();
      MidiaPlaylistPK midiaPlaylistPK = new MidiaPlaylistPK();
  
      midiaPlaylistPK.setIdMidia(midias.getIdMidia());
      midiaPlaylistPK.setIdPlaylist(instance.getIdPlaylist());                
      midiaPlaylist.setPk(midiaPlaylistPK);           
  
      midiaPlaylist.setNrOrdem(i++);
      midiaPlaylist.setMidia(midias); 
      midiaPlaylist.setPlaylist(instance);
  
      //playlistBean.salvar(midiaPlaylist);
      **midiaPlaylistBean.salvar(midiaPlaylist);** -&gt; error
  }   
  </code></pre>
  
  <p>Save the ID in the "playlist" table. And I make a for saving the ids of Midia in MidiaPlaylist.</p>
  
  <p><strong>My MidiaPlaylistBean</strong></p>
  
  <pre><code>@TransactionAttribute(TransactionAttributeType.REQUIRED)
  public MidiaPlaylist salvar(MidiaPlaylist entity){
      try{
          entity = super.save(entity);
      }catch(Exception e){
          e.printStackTrace();
      }
      return entity;
  }
  </code></pre>
  
  <p><strong>My MidiaPlaylist</strong></p>
  
  <pre><code>@EmbeddedId 
  private MidiaPlaylistPK pk = new MidiaPlaylistPK();
  
  public MidiaPlaylist(){
  }
  
  @MapsId("idMidia")
  @ManyToOne
  @JoinColumn(name="id_midia", referencedColumnName="id_midia")
  private Midia midia;
  
  @MapsId("idPlaylist")
  @ManyToOne
  @JoinColumn(name="id_playlist", referencedColumnName="id_playlist")
  private Playlist playlist;  
  
  @Column(name="nr_ordem", nullable=false)
  private int nrOrdem;
  
  
  public Midia getMidia() {
      return midia;
  }
  
  public void setMidia(Midia midia) {
      this.midia = midia;
  }
  
  public Playlist getPlaylist() {
      return playlist;
  }
  
  public void setPlaylist(Playlist playlist) {
      this.playlist = playlist;
  }
  
  public void setPk(MidiaPlaylistPK id) {
      this.pk = id;
  }
  
  public MidiaPlaylistPK getPk() {
      return this.pk;
  }
  
  public void setNrOrdem(int nrOrdem){
      this.nrOrdem = nrOrdem;
  }
  
  public int getNrOrdem(){
      return this.nrOrdem;
  }
  </code></pre>
  
  <p>My Playlist </p>
  
  <pre><code>@Id
  @GeneratedValue
  @Column(name="id_playlist")
  private Long idPlaylist;
  
  @OneToMany(mappedBy = "playlist", cascade=CascadeType.MERGE)
  private List&lt;MidiaPlaylist&gt; midiaPlaylist;
  --get et setter
  </code></pre>
  
  <p><strong>Console error</strong>:</p>
  
  <blockquote>
    <p>detached entity passed to persist: com.bcm.midia.database.entity.Midia</p>
  </blockquote>
  
  <p><strong>Where I think the error is</strong>: in mapping of Midia </p>
  
  <pre><code>@Id
  @GeneratedValue
  @Column(name="id_midia")
  private Long idMidia; 
  
  @OneToMany(mappedBy = "midia", fetch = FetchType.LAZY, cascade=CascadeType.MERGE)   
  private List&lt;MidiaPlaylist&gt; midiaPlaylist;
  --get et setter 
  </code></pre>
  
  <p>Can you help me understand how to save in this mapping many-to-many?
  Is it best to redo this relationship?
  I do not know what to do anymore.</p>