Rachid
Rachid

Reputation: 165

how to add other columns to the resulting table when using a @ManyToMany?

How can I add other columns in the resulting table from two entites that are mapped together with the @ManyToMany mapping annotation?

I have the two classes :

package com.rachid.jpa;

import java.io.Serializable;
import java.lang.Integer;
import java.lang.String;
import java.util.Collection;
import java.sql.Date;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: Project
 *
 */
@Entity
@Table(name="PROJECT")

public class Project implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idProj;

private String projectName;
private Date startDate;
private Date endDate;

@ManyToMany(mappedBy="projects")
private Collection<Employee> employees;
private static final long serialVersionUID = 1L;

@OneToOne
@JoinColumn(name="matriculeChef")
private Employee chef;


public Project() {
    super();
}   
public Integer getIdProj() {
    return this.idProj;
}

public void setIdProj(Integer idProj) {
    this.idProj = idProj;
}   
public String getProjectName() {
    return this.projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}   

public Date getStartDate() {
    return startDate;
}
public void setStartDate(Date startDate) {
    this.startDate = startDate;
}
public Date getEndDate() {
    return endDate;
}
public void setEndDate(Date endDate) {
    this.endDate = endDate;
}
public Collection<Employee> getEmployees() {
    return employees;
}
public void setEmployees(Collection<Employee> employees) {
    this.employees = employees;
}
public Employee getChef() {
    return chef;
}
public void setChef(Employee chef) {
    this.chef = chef;
}


    }

and

   package com.rachid.jpa;

   import java.io.Serializable;
   import java.lang.Integer;
   import java.util.Collection;
   import javax.persistence.*;

  /**
    * Entity implementation class for Entity: Employee
    *
    */
    @Entity
    @Table(name="EMPLOYEE")

    public class Employee implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idEmp;

@Embedded
private Personne personne;

@ManyToMany
@JoinTable(name="EMP_PROJ",
joinColumns = @JoinColumn(name="EMP_ID"),
inverseJoinColumns = @JoinColumn(name="PROJ_ID"))
private Collection<Project> projects;


@ManyToOne
@JoinColumn(name="matriculeChef")
private Employee chef;


@OneToMany(mappedBy="chef")
private Collection<Employee> manaedEmployees;


@OneToOne(mappedBy="employee")
private User user;

@OneToOne(mappedBy="chefEntite")
private Entite entite;

@OneToOne(mappedBy="chef")
private Project projet;

@OneToMany(mappedBy="employee")
private Collection<Conge> conges;

@ManyToOne
@JoinColumn(name="idEntite")
private Entite entiteTravail;

@ManyToMany
@JoinTable(name="EMP_FORM",
joinColumns = @JoinColumn(name="EMP_ID"),
inverseJoinColumns = @JoinColumn(name="FROM_ID"))
private Collection<Formation> formations;

private String fonction;

private static final long serialVersionUID = 1L;

public Employee() {
    super();
}   
public Integer getIdEmp() {
    return this.idEmp;
}

public void setIdEmp(Integer idEmp) {
    this.idEmp = idEmp;
}   

public Collection<Project> getProjects() {
    return projects;
}
public void setProjects(Collection<Project> projects) {
    this.projects = projects;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public Entite getEntite() {
    return entite;
}
public void setEntite(Entite entite) {
    this.entite = entite;
}
public Project getProjet() {
    return projet;
}
public void setProjet(Project projet) {
    this.projet = projet;
}
public Collection<Conge> getConges() {
    return conges;
}
public void setConges(Collection<Conge> conges) {
    this.conges = conges;
}
public Entite getEntiteTravail() {
    return entiteTravail;
}
public void setEntiteTravail(Entite entiteTravail) {
    this.entiteTravail = entiteTravail;
}
public Collection<Formation> getFormations() {
    return formations;
}
public void setFormations(Collection<Formation> formations) {
    this.formations = formations;
}
public Personne getPersonne() {
    return personne;
}
public void setPersonne(Personne personne) {
    this.personne = personne;
}
public Employee getChef() {
    return chef;
}
public void setChef(Employee chef) {
    this.chef = chef;
}
public Collection<Employee> getManaedEmployees() {
    return manaedEmployees;
}
public void setManaedEmployees(Collection<Employee> manaedEmployees) {
    this.manaedEmployees = manaedEmployees;
}
public String getFonction() {
    return fonction;
}
public void setFonction(String fonction) {
    this.fonction = fonction;
}


   }

I need to add two columns in the resulting table EMP_PROJ

thanks for help

Upvotes: 0

Views: 107

Answers (1)

SJuan76
SJuan76

Reputation: 24885

I would say than when you need to add attributes to an N-N relationship, you don't have an N-N relationship but a middle object with an 1-N relationship with each one.

Think of it in OOP terms... to which object would those properties belong? It could not be to either of the original ones, so a new one is needed.

Upvotes: 5

Related Questions