leandromoraesrj / sincad-backend

PROJETO BASE DE EXEMPLO USANDO AS MELHORES PRATICAS DE DESENVOLVIMENTO
0 stars 0 forks source link

Mapeamento Hibernate JPA - Classe Composta (PK) #25

Open leandromoraesrj opened 2 years ago

leandromoraesrj commented 2 years ago

@Embeddable

@Embeddable
public class EstabelecimentoPrincipalPK implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManyToOne
    @JoinColumn(name = "sq_unidade_empresarial")
    private UnidadeEmpresarial unidadeEmpresarial;
    @ManyToOne
    @JoinColumn(name = "sq_estabelecmento")
    private Estabelecimento estabelecimento;

Não tem construtores e é obrigatório ter hashcode e equals

@EmbeddedId

@Entity
@Table(name = "estabelecimento_principal")
public class EstabelecimentoPrincipal implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private EstabelecimentoPrincipalPK  id = new EstabelecimentoPrincipalPK();

O construtor terá não o ID, mas as classes contidas na PK; Além dos getters e setters do Id, colocar também para as classes contidas na PK; Nas classes contidas na PK, o mapeamento se dará através do @OneToMany(mappedBy = "id.unidadeEmpresarial")