I have two classes, that I want them to be ManyToMany relationship.
But not only that, I want to have a third class as a junction table, so that I could set its property “tipoContratante” , because each relationship could be of four kinds.
Is it possible using hibernate? Or would be another way to achieve the same?
@Entity
public class Contrato {
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy="contrato",cascade=CascadeType.ALL,orphanRemoval = true)
private List<Contratante> contratantes = new ArrayList<Contratante>();
}
@Entity
public class Contratante {
@Id
@GeneratedValue
private long id;
@Enumerated(EnumType.STRING)
private tipoContratante tipo;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "contrato_id")
private Contrato contrato;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "pessoa_id")
private Pessoa pessoa;
}
@Entity
public class Pessoa {
@OneToMany(mappedBy="pessoa",cascade = CascadeType.ALL)
private List<Contratante> contratos = new ArrayList<Contratante>();
}