具有额外列的多对多关联

时间:2017-07-18 18:12:58

标签: java hibernate jpa many-to-many hibernate-mapping

即使按照以下步骤提供的所有步骤,我也无法正确映射: https://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

CREATE TABLE `tb_pla_playlist` (
`id_playlist` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`nm_playlist` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id_playlist`) USING BTREE,
)
CREATE TABLE `tb_mid_midia` (
`id_midia` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`nm_midia` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id_midia`) USING BTREE
)
CREATE TABLE  `tb_mip_midia_playlist` (
`id_midia` INT(10) UNSIGNED NOT NULL,
`id_playlist` INT(10) UNSIGNED NOT NULL,
`nr_ordem` INT(10) UNSIGNED NOT NULL,
`CREATED_BY` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id_midia`,`CATEGORY_ID`),
CONSTRAINT `FK_id_midia` FOREIGN KEY (`id_midia`)
         REFERENCES `midia` (`id_midia`),
CONSTRAINT `FK_id_playlist` FOREIGN KEY (`id_playlist`)
         REFERENCES `playlist` (`id_playlist`)
)

playlist.java

@Entity
@Table(name="tb_pla_playlist")
public class Playlist implements Serializable {
private Set<MidiaPlaylist> midiaPlaylist = new HashSet<MidiaPlaylist>(); 

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_playlist")
private Long idPlaylist;

@OneToMany(mappedBy = "id.playlist", cascade = CascadeType.ALL)
private Set<MidiaPlaylist> getMidiaPlaylist(){
    return midiaPlaylist;
}
...
}

[错误:在属性'midiaPlaylist'中,“映射的”值'id.playlist'无法解析为目标实体上的属性。]

midia.java

@Entity
@Table(name="tb_mid_midia")
public class Midia implements Serializable {
private Set<MidiaPlaylist> midiaPlaylist = new HashSet<MidiaPlaylist>(); 

@Id
@GeneratedValue
@Column(name="id_midia")
private Long idMidia;

@OneToMany(mappedBy = "id.midia", cascade=CascadeType.ALL)  
public Set<MidiaPlaylist> getMidiasPlaylist() {
    return midiaPlaylist;
}

[错误:在属性'midiasPlaylist'中,“映射依据”值'id.midia'无法解析为目标实体上的属性。]

MidiaPlaylistPK.java

@Embeddable
public class MidiaPlaylistPK implements Serializable {
    private Midia midia;
private Playlist playlist;

@ManyToOne(cascade = CascadeType.ALL)
public Midia getMidia(){
    return midia;
}
public void setMidia(Midia midia){
    this.midia = midia;
}

@ManyToOne(cascade = CascadeType.ALL)
public Playlist getPlaylist(){
    return playlist;
}

public void setPlaylist(Playlist playlist){
    this.playlist = playlist;
}
}

MidiaPlaylist.java

@Entity
@Table(name="tb_mip_midia_playlist")
@AssociationOverrides({
   @AssociationOverride(name="id.playlist", [erro: Persistent type of override attribute "id.playlist" cannot be resolved]
   joinColumns = @JoinColumn(name = "id_playlist")),
   @AssociationOverride(name="id.midia", [erro: Persistent type of override attribute "id.midia" cannot be resolved]
   joinColumns = @JoinColumn(name = "id_midia"))
   })
 public class MidiaPlaylist implements Serializable {   
 private MidiaPlaylistPK id = new MidiaPlaylistPK();
 private int nrOrdem;

 @EmbeddedId    [erro: Embedded ID class should not contain relationship mappings]
 public MidiaPlaylistPK getId() {
    return this.id;
 }
@Column(name="nr_ordem", nullable=false)
public int getNrOrdem(){
    return this.nrOrdem;
}

@Transient
public Midia getMidia(){
    return getId().getMidia();
}

@Transient
public Playlist getPlaylist(){
    return getId().getPlaylist();
}

Relational model

有人可以帮我解决这些错误吗? TY!

1 个答案:

答案 0 :(得分:0)

您应该移除proc transpose data=data1; run;中的@ManyToOne次转发并使用@Embeddable @MapId的{​​{1}}内容,这里有一个示例: http://www.objectdb.com/api/java/jpa/MapsId