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: O comando e:
mvn clean generate-sources mvn clean generate-sources
En el pom.xml 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; package com.example.hibernate.model;
// Generated 24 feb 2026, 18:01:58 by Hibernate Tools 6.6.0.Final // Generated 24 feb 2026, 18:01:58 by Hibernate Tools 6.6.0.Final
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.FetchType; import jakarta.persistence.FetchType;
@@ -26,11 +25,9 @@ import java.util.Set;
* Emp generated by hbm2java * Emp generated by hbm2java
*/ */
@Entity @Entity
@Table(name="EMP" @Table(name = "EMP")
)
public class Emp implements java.io.Serializable { public class Emp implements java.io.Serializable {
private Integer empno; private Integer empno;
private Emp jefe; private Emp jefe;
private Dept dept; private Dept dept;
@@ -46,7 +43,8 @@ public class Emp implements java.io.Serializable {
public Emp() { 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) { 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.jefe = jefe;
this.dept = dept; this.dept = dept;
this.ename = ename; this.ename = ename;
@@ -59,8 +57,8 @@ public class Emp implements java.io.Serializable {
this.emps = emps; this.emps = emps;
} }
@Id @GeneratedValue(strategy=IDENTITY) @Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "EMPNO", unique = true, nullable = false) @Column(name = "EMPNO", unique = true, nullable = false)
public Integer getEmpno() { public Integer getEmpno() {
@@ -91,7 +89,6 @@ public class Emp implements java.io.Serializable {
this.dept = dept; this.dept = dept;
} }
@Column(name = "ENAME", length = 20) @Column(name = "ENAME", length = 20)
public String getEname() { public String getEname() {
return this.ename; return this.ename;
@@ -101,7 +98,6 @@ public class Emp implements java.io.Serializable {
this.ename = ename; this.ename = ename;
} }
@Column(name = "JOB", length = 20) @Column(name = "JOB", length = 20)
public String getJob() { public String getJob() {
return this.job; return this.job;
@@ -121,7 +117,6 @@ public class Emp implements java.io.Serializable {
this.hiredate = hiredate; this.hiredate = hiredate;
} }
@Column(name = "SAL", precision = 15, scale = 2) @Column(name = "SAL", precision = 15, scale = 2)
public BigDecimal getSal() { public BigDecimal getSal() {
return this.sal; return this.sal;
@@ -131,7 +126,6 @@ public class Emp implements java.io.Serializable {
this.sal = sal; this.sal = sal;
} }
@Column(name = "COMM", precision = 15, scale = 2) @Column(name = "COMM", precision = 15, scale = 2)
public BigDecimal getComm() { public BigDecimal getComm() {
return this.comm; return this.comm;
@@ -149,11 +143,16 @@ public class Emp implements java.io.Serializable {
public void setAccounts(Set<Account> accounts) { public void setAccounts(Set<Account> accounts) {
this.accounts = 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 = { * Eliminamos las JoinTable y JoinCloun por que o lado propietario e Project
@JoinColumn(name="EMPNO", nullable=false, updatable=false) }, inverseJoinColumns = { *
@JoinColumn(name="PROJECTNO", nullable=false, updatable=false) }) * @JoinTable(name="PROJECT_EMP", catalog="empresa", joinColumns = {
* @JoinColumn(name="EMPNO", nullable=false) }, inverseJoinColumns = {
* @JoinColumn(name="PROJECTNO", nullable=false) })
*/
public Set<Project> getProjects() { public Set<Project> getProjects() {
return this.projects; return this.projects;
} }
@@ -171,9 +170,4 @@ public class Emp implements java.io.Serializable {
this.emps = emps; this.emps = emps;
} }
} }

View File

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