Many to Many lado propietario Project

This commit is contained in:
2026-03-02 23:26:21 +01:00
parent 8c66150d45
commit b7f6a057f3
3 changed files with 66 additions and 69 deletions

View File

@@ -7,5 +7,6 @@ Indica no arquivo README.md na raíz do proxecto o comando que utilizaches (0,1
O comando e:
mvn clean generate-sources
En el pom.xml
<packageName>com.example.hibernate.model</packageName>
<packageName>com.example.hibernate.model</packageName>

View File

@@ -1,7 +1,6 @@
package com.example.hibernate.model;
// Generated 24 feb 2026, 18:01:58 by Hibernate Tools 6.6.0.Final
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
@@ -26,154 +25,149 @@ import java.util.Set;
* Emp generated by hbm2java
*/
@Entity
@Table(name="EMP"
)
public class Emp implements java.io.Serializable {
@Table(name = "EMP")
public class Emp implements java.io.Serializable {
private Integer empno;
private Emp jefe;
private Dept dept;
private String ename;
private String job;
private LocalDate hiredate;
private BigDecimal sal;
private BigDecimal comm;
private Set<Account> accounts = new HashSet<Account>(0);
private Set<Project> projects = new HashSet<Project>(0);
private Set<Emp> emps = new HashSet<Emp>(0);
private Integer empno;
private Emp jefe;
private Dept dept;
private String ename;
private String job;
private LocalDate hiredate;
private BigDecimal sal;
private BigDecimal comm;
private Set<Account> accounts = new HashSet<Account>(0);
private Set<Project> projects = new HashSet<Project>(0);
private Set<Emp> emps = new HashSet<Emp>(0);
public Emp() {
}
public Emp(Emp jefe, Dept dept, String ename, String job, Date hiredate, BigDecimal sal, BigDecimal comm, Set<Account> accounts, Set<Project> projects, Set<Emp> emps) {
this.jefe = jefe;
this.dept = dept;
this.ename = ename;
this.job = job;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.accounts = accounts;
this.projects = projects;
this.emps = emps;
public Emp(Emp jefe, Dept dept, String ename, String job, Date hiredate, BigDecimal sal, BigDecimal comm,
Set<Account> accounts, Set<Project> projects, Set<Emp> emps) {
this.jefe = jefe;
this.dept = dept;
this.ename = ename;
this.job = job;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.accounts = accounts;
this.projects = projects;
this.emps = emps;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="EMPNO", unique=true, nullable=false)
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "EMPNO", unique = true, nullable = false)
public Integer getEmpno() {
return this.empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="MGR")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MGR")
public Emp getJefe() {
return this.jefe;
}
public void setJefe(Emp jefe) {
this.jefe = jefe;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="DEPTNO")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPTNO")
public Dept getDept() {
return this.dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Column(name="ENAME", length=20)
@Column(name = "ENAME", length = 20)
public String getEname() {
return this.ename;
}
public void setEname(String ename) {
this.ename = ename;
}
@Column(name="JOB", length=20)
@Column(name = "JOB", length = 20)
public String getJob() {
return this.job;
}
public void setJob(String job) {
this.job = job;
}
// @Temporal(TemporalType.DATE)
@Column(name="HIREDATE", length=10)
@Column(name = "HIREDATE", length = 10)
public LocalDate getHiredate() {
return this.hiredate;
}
public void setHiredate(LocalDate hiredate) {
this.hiredate = hiredate;
}
@Column(name="SAL", precision=15, scale=2)
@Column(name = "SAL", precision = 15, scale = 2)
public BigDecimal getSal() {
return this.sal;
}
public void setSal(BigDecimal sal) {
this.sal = sal;
}
@Column(name="COMM", precision=15, scale=2)
@Column(name = "COMM", precision = 15, scale = 2)
public BigDecimal getComm() {
return this.comm;
}
public void setComm(BigDecimal comm) {
this.comm = comm;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="emp")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "emp")
public Set<Account> getAccounts() {
return this.accounts;
}
public void setAccounts(Set<Account> accounts) {
this.accounts = accounts;
}
// Añadese o atributo mapppedBy co atributo da clase do outro extremo da relación.
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "emps")
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="PROJECT_EMP", catalog="empresa", joinColumns = {
@JoinColumn(name="EMPNO", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="PROJECTNO", nullable=false, updatable=false) })
/*
* Eliminamos las JoinTable y JoinCloun por que o lado propietario e Project
*
* @JoinTable(name="PROJECT_EMP", catalog="empresa", joinColumns = {
* @JoinColumn(name="EMPNO", nullable=false) }, inverseJoinColumns = {
* @JoinColumn(name="PROJECTNO", nullable=false) })
*/
public Set<Project> getProjects() {
return this.projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="jefe")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "jefe")
public Set<Emp> getEmps() {
return this.emps;
}
public void setEmps(Set<Emp> emps) {
this.emps = emps;
}
}

View File

@@ -59,9 +59,11 @@ public class Project implements java.io.Serializable {
}
@ManyToMany(fetch = FetchType.LAZY)
// Nome da tabela intermedia, nome da base de datos
@JoinTable(name = "PROJECT_EMP", catalog = "empresa", joinColumns = {
@JoinColumn(name = "PROJECTNO", nullable = false, updatable = false) }, inverseJoinColumns = {
@JoinColumn(name = "EMPNO", nullable = false, updatable = false) })
// Nome da columna da tabela intermedia, nome da base de datos
@JoinColumn(name = "PROJECTNO", nullable = false) }, inverseJoinColumns = {
@JoinColumn(name = "EMPNO", nullable = false) })
public Set<Emp> getEmps() {
return this.emps;
}