Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

[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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...