As vezes no encontramos perante relacionamentos Trenarios como o da figura abaixo.
Com o JPA podemos mapea-lo conforme o codigo java deste post.
No final teremos uma quarta tabela que conterá os ID e campos definidos para ela.
@Entity
public class Professor extends Utente{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// gets e sets
}
@Entity
public class Disciplina extends ObjectoSae{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// gets e sets
}
@Entity
public class Turma extends ObjectoSae {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// gets e sets
}
@Entity
public class ArranjoTurma implements Serializable {
@EmbeddedId
private ArranjoTurmaPK pk;
@ManyToOne
@JoinColumn(name = "professorId", referencedColumnName = "id", insertable = false, updatable = false)
private Professor professor;
@ManyToOne
@NotNull(message = "Informe a disciplina")
@JoinColumn(name = "disciplinaId", referencedColumnName = "id", insertable = false, updatable = false)
private Disciplina disciplina;
@ManyToOne
@NotNull(message = "Informe a turma")
@JoinColumn(name = "turmaId", referencedColumnName = "id", insertable = false, updatable = false)
private Turma turma;
public ArranjoTurma() {
pk = new ArranjoTurmaPK();
}
public ArranjoTurmaPK getPk() {
return pk;
}
public void setPk(ArranjoTurmaPK pk) {
this.pk = pk;
}
public Professor getProfessor() {
return professor;
}
public void setProfessor(Professor professor) {
this.professor = professor;
if (this.professor != null) {
this.pk.setProfessorId(this.professor.getId());
}
}
public Disciplina getDisciplina() {
return disciplina;
}
public void setDisciplina(Disciplina disciplina) {
this.disciplina = disciplina;
this.pk.setDisciplinaId(this.disciplina.getId());
}
public Turma getTurma() {
return turma;
}
public void setTurma(Turma turma) {
this.turma = turma;
this.pk.setTurmaId(this.turma.getId());
}
}
@Embeddable
public class ArranjoTurmaPK implements Serializable{
@Column(name = "professorId")
private Long professorId;
@Column(name = "disciplinaId")
@NotNull(message = "Informe a disciplina")
private Long disciplinaId;
@Column(name = "turmaId")
@NotNull(message = "Informe a turma")
private Long turmaId;
public Long getProfessorId() {
return professorId;
}
public void setProfessorId(Long professorId) {
this.professorId = professorId;
}
public Long getDisciplinaId() {
return disciplinaId;
}
public void setDisciplinaId(Long disciplinaId) {
this.disciplinaId = disciplinaId;
}
public Long getTurmaId() {
return turmaId;
}
public void setTurmaId(Long turmaId) {
this.turmaId = turmaId;
}
@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + Objects.hashCode(this.professorId);
hash = 73 * hash + Objects.hashCode(this.disciplinaId);
hash = 73 * hash + Objects.hashCode(this.turmaId);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ArranjoTurmaPK other = (ArranjoTurmaPK) obj;
if (!Objects.equals(this.professorId, other.professorId)) {
return false;
}
if (!Objects.equals(this.disciplinaId, other.disciplinaId)) {
return false;
}
if (!Objects.equals(this.turmaId, other.turmaId)) {
return false;
}
return true;
}
}
Nenhum comentário:
Postar um comentário