我遇到了JPA和ManyToMany协会的问题。
我有两个类FOA_PARAM_EMPLOYE和FOA_PARAM_POSITION,以及一个关联表FOA_PARAM_EMPLOYE_POSITION。
Class FoaParamEmploye:
@Entity
@Table(name = "FOA_PARAM_EMPLOYE")
@NamedQuery(name = "FoaParamEmploye.findAll", query = "SELECT f FROM FoaParamEmploye f")
public class FoaParamEmploye implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private FoaParamEmployePK id;
@Column(name = "ACTEUR_MAJ_OCCUR")
private String acteurMajOccur;
@Column(name = "ADRESSE_EMAIL")
private String adresseEmail;
// bi-directional many-to-many association to FoaParamPosition
@ManyToMany
@JoinTable(
name = "FOA_PARAM_EMPLOYE_POSITION",
joinColumns = { @JoinColumn(name = "ID_EMPLOYE"),
@JoinColumn(name = "COD_ENTREP") },
inverseJoinColumns = { @JoinColumn(name = "ID_POSITION")
})
private List<FoaParamPosition> foaParamPositions;
public FoaParamEmployePK getId() {
return this.id;
}
public void setId(FoaParamEmployePK id) {
this.id = id;
}
public String getActeurMajOccur() {
return this.acteurMajOccur;
}
public void setActeurMajOccur(String acteurMajOccur) {
this.acteurMajOccur = acteurMajOccur;
}
public String getAdresseEmail() {
return this.adresseEmail;
}
public void setAdresseEmail(String adresseEmail) {
this.adresseEmail = adresseEmail;
}
public List<FoaParamPosition> getFoaParamPositions() {
return foaParamPositions;
}
public void setFoaParamPositions(List<FoaParamPosition> pFoaParamPositions) {
this.foaParamPositions = pFoaParamPositions;
}
}
Class FoaParamPosition:
@Entity
@Table(name="FOA_PARAM_POSITION")
@NamedQuery(name="FoaParamPosition.findAll", query="SELECT f FROM FoaParamPosition f")
public class FoaParamPosition implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private FoaParamPositionPK id;
@Column(name="ACTEUR_MAJ_OCCUR")
private String acteurMajOccur;
@Column(name="CD_PROFIL_AFFECTATION")
private String cdProfilAffectation;
// bi-directional many-to-many association to FoaParamEmploye
@ManyToMany
private List<FoaParamEmploye> foaParamEmployes;
public FoaParamPositionPK getId() {
return this.id;
}
public void setId(FoaParamPositionPK id) {
this.id = id;
}
public String getActeurMajOccur() {
return this.acteurMajOccur;
}
public void setActeurMajOccur(String acteurMajOccur) {
this.acteurMajOccur = acteurMajOccur;
}
public String getCdProfilAffectation() {
return this.cdProfilAffectation;
}
public void setCdProfilAffectation(String cdProfilAffectation) {
this.cdProfilAffectation = cdProfilAffectation;
}
public List<FoaParamEmploye> getFoaParamEmployes() {
return foaParamEmployes;
}
public void setFoaParamEmployes(List<FoaParamEmploye> pFoaParamEmployes) {
this.foaParamEmployes = pFoaParamEmployes;
}
}
表FOA_PARAM_EMPLOYE_POSITION有以下列:
COD_ENTREP
ID_EMPLOYE
ID_POSITION
XQCIF
ACTEUR_MAJ_OCCUR
DATE_HEURE_MAJ_OCCUR
我遇到了这个例外:
A Foreign key refering com.groupama.middlgan.entities.FoaParamPosition from
com.groupama.middlgan.entities.FoaParamEmploye has the wrong number of column.
should be 2
如果我在我的FoaParamEmploye实体的inverseJoinColumns
上添加COD_ENTREP,我得到了这个例外:
Repeated column in mapping for collection:
com.groupama.middlgan.entities.FoaParamEmploye.foaParamPositions column: COD_ENTREP
任何想法?
答案 0 :(得分:0)
[我假设您列出了关联表FOA_PARAM_EMPLOYE_POSITION
的列,而不是FOA_PARAM_EMPLOYE
,正如您在问题中所述。]
您的映射暗示FOA_PARAM_EMPLOYE_POSITION.COD_ENTREP
是两个外键的一部分,一个引用FOA_PARAM_EMPLOYE
,另一个引用FOA_PARAM_POSITION
。实际上,这两个外键可能包含COD_ENTREP
的相同值,但数据库或JPA无法强制执行此操作。
您可能应该以不同的方式对关系进行建模,可能会添加另一个类似于容器的对象,该对象与FoaParamEmploye
和FoaParamPosition
具有双向一对多关系并共享其主要部分键与其他两个主键。
答案 1 :(得分:0)
如果要保留inverseJoinColumn映射,可以执行以下操作,
@ManyToMany
@JoinTable(
name = "FOA_PARAM_EMPLOYE_POSITION",
joinColumns = { @JoinColumn(name = "ID_EMPLOYE"),
@JoinColumn(name = "COD_ENTREP") }
,
inverseJoinColumns = {@JoinColumn(name = "FOAPARAMPOSITION_COD_ENTREP"), @JoinColumn(name = "FOAPARAMPOSITION_ID_POSITION")}
)
private List<FoaParamPosition> foaParamPosition;
和
@ManyToMany
@JoinTable(
name = "FOA_PARAM_EMPLOYE_POSITION",
joinColumns = { @JoinColumn(name = "ID_POSITION"),
@JoinColumn(name = "COD_ENTREP") }
,
inverseJoinColumns = {@JoinColumn(name = "FOAPARAMEMPLOYE_COD_ENTREP"), @JoinColumn(name = "FOAPARAMEMPLOYE_ID_EMPLOYE")}
)
private List<FoaParamEmploye> foaParamEmploye;
映射与您在其他问题JPA Many To Many Select
中发布的代码一致