我遇到了问题,因为我使用中间表来保存其他2个表(MidiaPlaylist的播放列表和Midia)的ID。对于多对一/一对多的关系。
当我使用带有额外列的MapID时,我创建了两个多对一关系。我使用@EmbeddedId表示法来创建ID的关系。映射类型无法解析类型为“MERGE”。
表Midiaid_midia
--------
表MidiaPlaylist
id_playlist
id_midia
nr_order
表播放列表
id_playlist
-------
我的播放列表控制器
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);** -> error
}
将ID保存在“播放列表”表中。我在MidiaPlaylist中拯救了Midia的ids。
我的MidiaPlaylistBean
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public MidiaPlaylist salvar(MidiaPlaylist entity){
try{
entity = super.save(entity);
}catch(Exception e){
e.printStackTrace();
}
return entity;
}
我的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;
}
我的播放列表
@Id
@GeneratedValue
@Column(name="id_playlist")
private Long idPlaylist;
@OneToMany(mappedBy = "playlist", cascade=CascadeType.MERGE)
private List<MidiaPlaylist> midiaPlaylist;
--get et setter
控制台错误:
传递给persist的分离实体:com.bcm.midia.database.entity.Midia
我认为错误在哪里:在Midia的映射中
@Id
@GeneratedValue
@Column(name="id_midia")
private Long idMidia;
@OneToMany(mappedBy = "midia", fetch = FetchType.LAZY, cascade=CascadeType.MERGE)
private List<MidiaPlaylist> midiaPlaylist;
--get et setter
你能帮助我理解如何在这个映射中保存多对多吗? 重做这种关系最好吗? 我不知道该怎么办了。
答案 0 :(得分:0)
midia播放列表
public class MidiaPlaylist implements Serializable {
@EmbeddedId
private MidiaPlaylistPK pk;
@ManyToOne
@JoinColumn(name="id_midia", updatable = false, insertable = false)
private Midia midia;
@ManyToOne
@JoinColumn(name="id_playlist", updatable = false, insertable = false)
private Playlist playlist;
@Column(name="nr_ordem", nullable=false)
private int nrOrdem;
--GET AND SET
}
MidiaPlaylistPK
@Column(name = "id_midia")
private Long idMidia;
@Column(name = "id_playlist")
private Long idPlaylist;
public MidiaPlaylistPK(){
}
播放列表
//mapping
@Transient
private List<PlaylistProgramacao> listTerminal;
Midia
@Transient
private List<MidiaPlaylist> midiaPlaylist = new ArrayList<MidiaPlaylist>();
PlaylistController
try{
instance = playlistBean.salvarPlaylist(referenceValue);
}catch(Exception e){
e.printStackTrace();
}
int i = 1;
for(Midia midias : dualListMidias.getTarget()){
MidiaPlaylist midiaPlaylist1 = new MidiaPlaylist(midias, instance, i++);
midiaPlaylistBean.salvar(midiaPlaylist1);
}
这解决了我的问题,即使用hibernate和JPA持久使用带有额外列的多对多映射。