Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

jpa - A Foreign key refering has wrong number of columns

I got a problem with JPA and ManyToMany association.

I got two class FOA_PARAM_EMPLOYE and FOA_PARAM_POSITION, and an association table 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;
}
}

Table FOA_PARAM_EMPLOYE_POSITION has this columns :

COD_ENTREP
ID_EMPLOYE
ID_POSITION
XQCIF
ACTEUR_MAJ_OCCUR
DATE_HEURE_MAJ_OCCUR

I got this exception :

A Foreign key refering com.groupama.middlgan.entities.FoaParamPosition from
com.groupama.middlgan.entities.FoaParamEmploye has the wrong number of column. 
should be 2

If I add COD_ENTREP on inverseJoinColumns in my FoaParamEmploye entity, I got this exception :

Repeated column in mapping for collection: 
com.groupama.middlgan.entities.FoaParamEmploye.foaParamPositions column: COD_ENTREP

Any idee ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

[I am assuming you are listing the columns for the association table FOA_PARAM_EMPLOYE_POSITION, not FOA_PARAM_EMPLOYE, as you state in your question.]

Your mappings imply FOA_PARAM_EMPLOYE_POSITION.COD_ENTREP is part of two foreign keys, one referencing FOA_PARAM_EMPLOYE and one referencing FOA_PARAM_POSITION. In practice these two foreign keys might contain the same value for COD_ENTREP, but this cannot be enforced by the database or JPA.

You should probably model the relationship differently, possibly add another, container-like, object that has bi-directional one-to-many relationships with both FoaParamEmploye and FoaParamPosition and shares parts of its primary key with the other two primary keys.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...