Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 外键引用的列数错误_Jpa - Fatal编程技术网

Jpa 外键引用的列数错误

Jpa 外键引用的列数错误,jpa,Jpa,我对JPA和许多协会有意见 我得到了两个类FOA_PARAM_EMPLOYE和FOA_PARAM_POSITION,以及一个关联表FOA_PARAM_EMPLOYE_POSITION FoaParamEmploye类: @Entity @Table(name = "FOA_PARAM_EMPLOYE") @NamedQuery(name = "FoaParamEmploye.findAll", query = "SELECT f FROM FoaParamEmploye f") public c

我对JPA和许多协会有意见

我得到了两个类FOA_PARAM_EMPLOYE和FOA_PARAM_POSITION,以及一个关联表FOA_PARAM_EMPLOYE_POSITION

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;
}
}
我得到了一个例外:

A Foreign key refering com.groupama.middlgan.entities.FoaParamPosition from
com.groupama.middlgan.entities.FoaParamEmploye has the wrong number of column. 
should be 2
Repeated column in mapping for collection: 
com.groupama.middlgan.entities.FoaParamEmploye.foaParamPositions column: COD_ENTREP
如果我在FoaParamEmploye实体的inverseJoinColumns中添加COD_ENTREP,我会得到以下异常:

A Foreign key refering com.groupama.middlgan.entities.FoaParamPosition from
com.groupama.middlgan.entities.FoaParamEmploye has the wrong number of column. 
should be 2
Repeated column in mapping for collection: 
com.groupama.middlgan.entities.FoaParamEmploye.foaParamPositions column: COD_ENTREP

有什么想法吗?

[我假设您列出的是关联表FOA_PARAM_EMPLOYE_POSITION的列,而不是您在问题中所述的FOA_PARAM_EMPLOYE。]

映射意味着FOA_PARAM_EMPLOYE_POSITION.COD_ENTREP是两个外键的一部分,一个引用FOA_PARAM_EMPLOYE,另一个引用FOA_PARAM_POSITION。实际上,这两个外键可能包含相同的COD_ENTREP值,但这不能由数据库或JPA强制执行


您可能应该以不同的方式对关系进行建模,可能会添加另一个类似容器的对象,该对象与FoaParamEmploye和FoaParamPosition具有双向一对多关系,并将其主键的一部分与其他两个主键共享。

如果您希望保持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;


映射与您在另一个问题中发布的代码一致

我通过删除FoaParamEmploye中的inverseJoinColumns,并在FoaParamPosition中添加@JoinTable而不添加inverseJoinColumns来找到解决方案。
@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;