java - 使用@ManyToMany 时如何将其他列添加到结果表中?

标签 java mysql jpa

如何从使用@ManyToMany 映射注释映射在一起的两个实体向结果表中添加其他列?

我有两个类:

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;
}


    }

   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;
}


   }

我需要在结果表 EMP_PROJ 中添加两列

感谢帮助

最佳答案

我想说的是,当您需要将属性添加到 N-N 关系时,您没有 N-N 关系,而是一个与每个对象都具有 1-N 关系的中间对象。

用 OOP 的术语来思考……这些属性属于哪个对象?它不可能是原来的任何一个,所以需要一个新的。

关于java - 使用@ManyToMany 时如何将其他列添加到结果表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12548049/

相关文章:

java - 线程重新运行run()方法

mysql - 创建程序错误 weekday()

python - SQL:如何将两列 GROUP BY 作为无序对?

java - 如何通过 JDBC 实现持久化相关(级联)对象

java - 使用数据表与单独的审计表进行审计

java - 表不是由 Hibernate 创建的

java - 如何从 Java 中的 for 循环返回多个字符串?

java - String.format 不完全工作?

java - 用于国际化 i18n 测试的 Eclipse 插件

Mysql DISTINCT account_id 和 ORDER BY