gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:54:18 25 4
gpt4 key购买 nike

如何从使用@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com