0
Hello, I have a problem because I use an intermediate table to save the Ids of other 2 tables. Except that the way that Mapeei, I can’t save in this table because apparently JPA understands that I can’t pass the null entity nor pass the filled object to q it does a "MERGE" without change, follows:
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);
}
Saved in "playlist" table so you have the ID I want and the media Ids are already in the list dualListMidias.getTarget()
, then I do a for to record all id.playlist and id.midia in the media table
My Midiaplaylistbean
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public MidiaPlaylist salvar(MidiaPlaylist entity){
try{
entity = super.save(entity);
}catch(Exception e){
e.printStackTrace();
}
return entity;
}
My Midiaplaylist
@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 mistake is:
Detached Entity passed to persist: com.bcm.midia.database.Entity.Midia
Where I think the mistake is: in the mapping of Midia
@Id
@GeneratedValue
@Column(name="id_midia")
private Long idMidia;
@OneToMany(mappedBy = "midia", fetch = FetchType.LAZY, cascade=CascadeType.MERGE)
private List<MidiaPlaylist> midiaPlaylist;
Could someone explain to me why I can’t just save in the Midiaplaylist table this way of mapping Many-to-Many?
entity = super.save(entity);
- What does this methodsave
?– Victor Stafusa
Method extending from an abstract (extends Bussinessabstract<Midiaplaylist>) to persist, basically a getDAO(). save(Entity).
– Arcanjo Gabriel
Where is the setIdPlaylist method? Do not put the code...
– karanalpe
I added it now in the edition. The Midiaplaylistpk class stores the long of the playlist and media ids
– Arcanjo Gabriel
You make this set: midiaPlaylistPK.setIdPlaylist(instance.getIdPlaylist()). Where is the method?
– karanalpe
Add the class. In this case it is a relation Many to Many between playlist and midia where the Ids are saved and order in the Midiaplaylist table. I can easily consult with this mapping. the problem is recording
– Arcanjo Gabriel