Created
May 6, 2024 11:52
-
-
Save rcaneppele/37783429fc8379b8c3cfd8d18d2f3e88 to your computer and use it in GitHub Desktop.
Mapeamento ManyToMany com colunas adicionais na JPA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name = "estudantes") | |
public class Estudante { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
private String nome; | |
//outros atributos... | |
@OneToMany(mappedBy = "estudante", cascade = CascadeType.ALL, orphanRemoval = true) | |
private List<Matricula> matriculas = new ArrayList<>(); | |
public void registrarMatricula(Turma turma) { | |
Matricula matricula = new Matricula(this, turma); | |
matriculas.add(matricula); | |
turma.getMatriculas().add(matricula); | |
} | |
public void removerMatricula(Turma turma) { | |
for (Iterator<Matricula> iterator = matriculas.iterator(); iterator.hasNext();) { | |
Matricula matricula = iterator.next(); | |
if (matricula.getEstudante().equals(this) && matricula.getTurma().equals(turma)) { | |
iterator.remove(); | |
matricula.getTurma().getMatriculas().remove(matricula); | |
matricula.setEstudante(null); | |
matricula.setTurma(null); | |
} | |
} | |
} | |
//getters, setters, equals, hashCode, etc. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name = "matriculas") | |
public class Matricula { | |
@EmbeddedId | |
private MatriculaId id; | |
@ManyToOne(fetch = FetchType.LAZY) | |
@MapsId("estudanteId") | |
private Estudante estudante; | |
@ManyToOne(fetch = FetchType.LAZY) | |
@MapsId("turmaId") | |
private Turma turma; | |
private LocalDateTime data = LocalDateTime.now(); | |
//outros atributos | |
private Matricula() {} | |
public Matricula(Estudante estudante, Turma turma) { | |
this.estudante = estudante; | |
this.turma = turma; | |
this.id = new MatriculaId(estudante.getId(), turma.getId()); | |
} | |
//getters, setters, equals, hashCode, etc. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Embeddable | |
public class MatriculaId implements Serializable { | |
@Column(name = "estudante_id") | |
private Long estudanteId; | |
@Column(name = "turma_id") | |
private Long turmaId; | |
private MatriculaId() {} | |
public MatriculaId(Long estudanteId, Long turmaId) { | |
this.estudanteId = estudanteId; | |
this.turmaId = turmaId; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) | |
return false; | |
MatriculaId that = (MatriculaId) o; | |
return Objects.equals(estudanteId, that.estudanteId) && | |
Objects.equals(turmaId, that.turmaId); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(estudanteId, turmaId); | |
} | |
//getters, setters, etc. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name = "turmas") | |
public class Turma { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
private String codigo; | |
//outros atributos... | |
@OneToMany(mappedBy = "turma", cascade = CascadeType.ALL, orphanRemoval = true) | |
private List<Matricula> matriculas = new ArrayList<>(); | |
//getters, setters, equals, hashCode, etc. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment