0

J'ai une @Entity appelé équipe commerelation @OneToMany avec la clef primaire Hibernate

@Entity 
    @Table(name="projects_participants") 
    public class Team { 

     @Id`enter code here` 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     @Column 
     private int id; 



    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 


    @Column(name="participants_id") 
    private int participants_id; 

    public int getParticipants_id() { 
     return participants_id; 
    } 

    public void setParticipants_id(int participants_id) { 
     this.participants_id = participants_id; 
    } 









    /* 
    @CollectionOfElements 
    private Set<Projects> projectsParticipant; 

    @ManyToMany(fetch = FetchType.LAZY,mappedBy = "team") 
    public Set<Projects> getProjectsParticipant() { 
     return projectsParticipant; 
    } 


    public void setProjectsParticipant(Set<Projects> projectsParticipant) { 
     this.projectsParticipant = projectsParticipant; 
    } 
     */ 
} 

J'ai un autre nom de haricot Projets comme

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
@JoinTable(name = "projects_participants", joinColumns = { 
     @JoinColumn(name = "project_id") }, 
     inverseJoinColumns = { @JoinColumn(name = "participants_id",referencedColumnName="participants_id")}) 
public Set<Team> getTeam() { 
    return team; 
} 

public void setTeam(Set<Team> team) { 
    this.team = team; 
} 

Il fonctionne très bien, mais le problème La table project_participants a l'ID du projet et l'ID de la table d'équipe. Mais je veux project_id et Participants_id de la table d'équipe. project_participants Tableau est comme ci-dessous

enter image description here

table participants * enter image description here

Enfin ce que je veux est la table project_participants devrait avoir project_project_id et participants_id (non team_id) .Thanx à l'avance.

Répondre

0

Il me semble, que vous essayez de modéliser une relation m: n, mais n'utilisez pas l'annotation et les principes corrects.

1) Utilisez @ManyToMany en combinaison avec @JoinTable pour modéliser la relation m: n comme documenté dans jee5 api.

2) Ne modélisez pas les relations avec d'autres objets via des colonnes ID comme vous l'avez fait avec participants_id. Avec JPA, vous travaillez avec des objets, pas avec leurs ID, donc votre colonne serait un Participant.

Questions connexes