Doubts about building and relationships between classes and mapping in JPA

0

InthefollowingclassdiagramIamtryingtomakethefollowingrelationshipsbetweenclasswhereademandwillhavearesponsibleanalystandarequestingclient.Iwouldliketoknowifmymappingissureifthelistgetsintheclassdemandsinsteadthefollowingmodelsfollow:

packagebr.com.deivsoft.model;importjava.util.Date;importjava.util.List;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.EnumType;importjavax.persistence.Enumerated;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.JoinTable;importjavax.persistence.ManyToOne;importjavax.persistence.OneToMany;importjavax.persistence.Table;importjavax.persistence.Temporal;importjavax.persistence.TemporalType;importbr.com.deivsoft.enums.PrioridadeEnum;importbr.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;
    }
}

Class Customer is the one below in the code I called contact

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;
    }
}
    
asked by anonymous 10.02.2018 / 07:35

1 answer

1
  

In the following class diagram I'm trying to make the following relationships between class where a demand will have a responsible analyst and a requesting client . I would like to know if my mapping is right if the list gets in the class demands instead ...

Based on what you wrote in your question, and more specifically on the part I highlighted, and the class diagram you posted (where you can see relationships 1 -> 0 .. *, where Client and Analyst are marked with 1 and Demand is marked with 0 .. *), I would say that the 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 Requests you have requested, and analyst will have the list of Requests for which you were responsible.

So, the mapping looks like this:

Class Requirement:

@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;
    }
}

Contact Class:

@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 to be able to visualize the solution broadly and can reproduce when you need it, so I recommend you look for some material on the subject to get started you can check out these links:

DevMedia
Hibernate documentation
Baeldung

And not to mention that you probably at one time or another, you will have to define strategies for Cascade and Fetch for collections, subjects that I did not address because it is beyond the scope of your question.

    
10.02.2018 / 15:58