Hello, I have a problem because I use an intermediate table to save the IDs of 2 other tables. I'm not sure what to do with JPA, but I'm not sure what to do, but I'm not sure how to do this.
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);
}
Except in the "playlist" table so that I have the ID that I want and the media IDs are already in the dualListMidias.getTarget()
list, then I'm going to write all the id.playlist and id.midia in the midiaPlaylist table
My MediaPlaylistBean
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public MidiaPlaylist salvar(MidiaPlaylist entity){
try{
entity = super.save(entity);
}catch(Exception e){
e.printStackTrace();
}
return entity;
}
My MediaPlaylist
@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;
}
Playlist.java
@Id
@GeneratedValue
@Column(name="id_playlist")
private Long idPlaylist;
@OneToMany(mappedBy = "playlist", fetch=FetchType.LAZY)
private List<MidiaPlaylist> midiaPlaylist;
public List<MidiaPlaylist> getMidiaPlaylist(){
return midiaPlaylist;
}
public void setMidiaPlaylist(List<MidiaPlaylist> midiaPlaylist){
this.midiaPlaylist = midiaPlaylist;
}
public Playlist() {
}
public Long getIdPlaylist() {
return this.idPlaylist;
}
public void setIdPlaylist(Long idPlaylist) {
this.idPlaylist = idPlaylist;
}
The error is :
detached entity passed to persist: com.bcm.midia.database.entity.Midia
Where I think the error is : in Midia mapping
@Id
@GeneratedValue
@Column(name="id_midia")
private Long idMidia;
@OneToMany(mappedBy = "midia", fetch = FetchType.LAZY, cascade=CascadeType.MERGE)
private List<MidiaPlaylist> midiaPlaylist;
Can anyone explain to me why I can not just save in the MidiaPlaylist table from this many-to-many mapping form?