Doubts about the construction and relationships between classes and the mapping in JPA

Asked

Viewed 203 times

0

inserir a descrição da imagem aqui

In the following class diagram I am trying to make the following class relationships where a demand will have a responsible analyst and a requesting client. I wonder if my mapping is right if the list gets in the class demands otherwise follows the models below:

package br.com.deivsoft.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import br.com.deivsoft.enums.PrioridadeEnum;
import br.com.deivsoft.enums.StatusEnum;

@Entity
@Table(name = "DEMANDAS")
public class Demanda {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_DEMANDAS")
    private Long id;

    @Column(name = "NRO_TICKET", length = 50, nullable = false)
    private String nro_ticket;

    /* Enum de PRIORIDADE */
    @Enumerated(EnumType.ORDINAL)
    private PrioridadeEnum prioridade;

    /* Enum de STATUS */
    @Enumerated(EnumType.ORDINAL)
    private StatusEnum status;

    @OneToMany
    @JoinTable(name = "DEMANDA_ANALISTA", joinColumns = @JoinColumn(name = "ID_ANALITAS"), inverseJoinColumns = @JoinColumn(name = "ID_DEMANDAS"))
    private List<Contato> contato;

    @OneToMany
    @JoinTable(name = "DEMANDA_ANALISTA", joinColumns = @JoinColumn(name = "ID_ANALITAS"), inverseJoinColumns = @JoinColumn(name = "ID_DEMANDAS"))
    private List<Analista> analista;

    @Temporal(TemporalType.DATE)
    private Date dt_abertura;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNro_ticket() {
        return nro_ticket;
    }

    public void setNro_ticket(String nro_ticket) {
        this.nro_ticket = nro_ticket;
    }

    public StatusEnum getStatus() {
        return status;
    }

    public void setStatus(StatusEnum status) {
        this.status = status;
    }

    public PrioridadeEnum getPrioridade() {
        return prioridade;
    }

    public void setPrioridade(PrioridadeEnum prioridade) {
        this.prioridade = prioridade;
    }

    public List<Contato> getContato() {
        return contato;
    }

    public void setContato(List<Contato> contato) {
        this.contato = contato;
    }

    public List<Analista> getAnalista() {
        return analista;
    }

    public void setAnalista(List<Analista> analista) {
        this.analista = analista;
    }

    public void setDt_abertura(Date dt_abertura) {
        this.dt_abertura = dt_abertura;
    }
}



package br.com.deivsoft.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import br.com.deivsoft.model.Equipe.grupamentoEnum;

@Entity
@Table(name ="ANALISTAS")

public class Analista {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_ANALISTAS")
    private Long id;

    @Column(name = "MATRICULA", length = 15, nullable = false)
    private String matricula;

    @Column(name = "NOME", length = 255, nullable = false)
    private String nome;

    @Column(name = "EQUIPE", length = 150, nullable = false)
    private grupamentoEnum equipe;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public grupamentoEnum getEquipe() {
        return equipe;
    }

    public void setEquipe(grupamentoEnum equipe) {
        this.equipe = equipe;
    }

    public List<Demanda> getDemanda() {
        return demanda;
    }

    public void setDemanda(List<Demanda> demanda) {
        this.demanda = demanda;
    }
}

Customer class is the below in the contact call code

package br.com.deivsoft.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "CONTATOS")
public class Contato {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_CONTATOS")
    private Long Id;

    @Column(name = "NOME", length = 155, nullable = false)
    private String nome;

    @Column(name = "MATRICULA", length = 20, nullable = false)
    private String matricula;

    @Column(name = "LOTACAO", length = 155, nullable = false)
    private String lotacao;

    @OneToMany
    @JoinTable(name="DEMANDA_CONTATO",
               joinColumns=@JoinColumn(name="ID_CONTATOS"),
               inverseJoinColumns = @JoinColumn(name="ID_DEMANDAS"))
    private List<Demanda> demanda;

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getLotacao() {
        return lotacao;
    }

    public void setLotacao(String lotacao) {
        this.lotacao = lotacao;
    }

    public List<Demanda> getDemanda() {
        return demanda;
    }

    public void setDemanda(List<Demanda> demanda) {
        this.demanda = demanda;
    }
}

1 answer

1


In the following class diagram I am trying to make the following class relationships where a demand will have a responsible analyst and a requesting client. I wonder if my mapping is right if the list gets in the class demands the opposite...

Based on what you wrote in your question, and more specifically on the section I highlighted, and the class diagram you posted (where you can see relationships 1 -> 0.*, where Customer and Analyst are marked with 1 and Demand is marked with 0.. *), I would say that demand will have only one customer and one analyst, and the latter will have a list of Demands, both customer will have the list of demands you requested, and analyst will have the list of demands you were responsible for.

So the mapping would be like this:

Classe Demanda:

@Entity
@Table(name = "DEMANDAS")
public class Demanda {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_DEMANDAS")
    private Long id;

    @Column(name = "NRO_TICKET", length = 50, nullable = false)
    private String nro_ticket;

    /* Enum de PRIORIDADE */
    @Enumerated(EnumType.ORDINAL)
    private PrioridadeEnum prioridade;

    /* Enum de STATUS */
    @Enumerated(EnumType.ORDINAL)
    private StatusEnum status;

    @ManyToOne
    @JoinColumn(name = "ID_CONTATOS")
    private Contato contato;

    @ManyToOne
    @JoinColumn(name = "ID_ANALISTAS")
    private Analista analista;

    @Temporal(TemporalType.DATE)
    private Date dt_abertura;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNro_ticket() {
        return nro_ticket;
    }

    public void setNro_ticket(String nro_ticket) {
        this.nro_ticket = nro_ticket;
    }

    public StatusEnum getStatus() {
        return status;
    }

    public void setStatus(StatusEnum status) {
        this.status = status;
    }

    public PrioridadeEnum getPrioridade() {
        return prioridade;
    }

    public void setPrioridade(PrioridadeEnum prioridade) {
        this.prioridade = prioridade;
    }

    public List<Contato> getContato() {
        return contato;
    }

    public void setContato(List<Contato> contato) {
        this.contato = contato;
    }

    public List<Analista> getAnalista() {
        return analista;
    }

    public void setAnalista(List<Analista> analista) {
        this.analista = analista;
    }

    public void setDt_abertura(Date dt_abertura) {
        this.dt_abertura = dt_abertura;
    }
}

Analyst class:

@Entity
@Table(name ="ANALISTAS")
public class Analista {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_ANALISTAS")
    private Long id;

    @Column(name = "MATRICULA", length = 15, nullable = false)
    private String matricula;

    @Column(name = "NOME", length = 255, nullable = false)
    private String nome;

    @Column(name = "EQUIPE", length = 150, nullable = false)
    private grupamentoEnum equipe;

   @OneToMany(mappedBy = "analista")// atributo analista da classe Demandas
   private List<Demanda> demandas;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public grupamentoEnum getEquipe() {
        return equipe;
    }

    public void setEquipe(grupamentoEnum equipe) {
        this.equipe = equipe;
    }

    public List<Demanda> getDemandas() {
        return demanda;
    }

    public void setDemandas(List<Demanda> demandas) {
        this.demandas = demandas;
    }
}

Class Contact:

@Entity
@Table(name = "CONTATOS")
public class Contato {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_CONTATOS")
    private Long Id;

    @Column(name = "NOME", length = 155, nullable = false)
    private String nome;

    @Column(name = "MATRICULA", length = 20, nullable = false)
    private String matricula;

    @Column(name = "LOTACAO", length = 155, nullable = false)
    private String lotacao;

    @OneToMany
    @OneToMany(mappedBy = "contato")/atributo contato da Classe Demanda
    private List<Demanda> demandas;

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getLotacao() {
        return lotacao;
    }

    public void setLotacao(String lotacao) {
        this.lotacao = lotacao;
    }

    public List<Demanda> getDemanda() {
        return demanda;
    }

    public void setDemanda(List<Demanda> demanda) {
        this.demanda = demanda;
    }
}

This is a type of subject that only copy and paste does not solve much, it is interesting to try to understand the concepts, and thus be able to visualize the solution broadly and can reproduce when you need, so I recommend that you look for some material on the subject, to get started you can check these links:

Devmedia
Documentación Hibernate
Baeldung

And not to mention that you’re probably gonna have to define strategies of Cascade and Fetch for the collections, subjects that I didn’t address it because it’s outside the scope of your question.

  • Resende vc could show me what the relational diagram of the database would look like in this case?

  • In fact there is not much secret, demand will have a foreing key for customer and another for analyst. Note that it is not a relationship N to N, but 1 to N, so it is not necessary to use union table for either relationship, only the foreing Keys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.