I am doing a project with JPA for a quotation system and I have doubts on how to build the relationship between classes, and would like (if possible) some opinions.
My project is a quotation system, where the company will create a quote and select which partners and sectors will be part of it. At first I created the following entities:
- Quotation: will contain the quotation header;
- Partner: will contain the registration of all partners;
- Sector: it will contain the sectors register;
- Items: will contain the list of items that are linked to the sectors;
- Quoted Items: will contain the release of quantities and values entered by the partners.
My biggest question right now is how to build the relationship between them. I tried to build a relationship this way:
-
Cotacao
will contain a list of the entityParceiro
with@OneToMany
(a quote may have one or more partners); -
Cotacao
will contain a list of the entitySetor
with@OneToMany
(a quotation may have one or more sectors); -
Setor
will contain a list of the entityItens
with@OneToMany
(a sector may have one or more items); -
Parceiro
will contain a list of entityItensLancados
(a partner may have one or more items thrown).
When I run the project and see how BD was created, I can not find a relationship in the tables between the item that was posted with the item, nor between the item posted with the quote that the partner entered.
Would anyone have any tips on how to build this relationship?
The classes for analysis follow.
Quotation :
@Entity
@SequenceGenerator(name = "SEQ_COTACAO", sequenceName = "SEQ_COTACAO", initialValue = 1)
public class Cotacao implements Serializable {
private static final long serialVersionUID = 2871116154931914363L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_COTACAO")
private Integer codCotacao;
@Temporal(TemporalType.TIMESTAMP)
private Date dataInicio;
@Temporal(TemporalType.TIMESTAMP)
private Date dataFim;
@OneToMany(mappedBy = "cotacao")
private List<ItensLancados> itensLancados;
@ManyToMany
@JoinTable(name = "cotacao_parceiro", joinColumns = @JoinColumn(name = "cotacao_id"), inverseJoinColumns = @JoinColumn(name = "parceiro_id"))
private List<Parceiro> parceiros;
@ManyToMany
@JoinTable(name = "cotacao_setor", joinColumns = @JoinColumn(name = "cotacao_id"), inverseJoinColumns = @JoinColumn(name = "setor_id"))
private List<Setor> setores;
}
Sector :
@Entity
@SequenceGenerator(name = "SEQ_SETOR", sequenceName = "SEQ_SETOR", initialValue = 1)
public class Setor implements Serializable {
private static final long serialVersionUID = -1651773345231721498L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_SETOR")
private Integer codSetor;
@OneToMany(mappedBy = "setor")
private List<Itens> itens;
@ManyToMany(mappedBy = "setores")
private List<Cotacao> cotacoes;
}
Items :
@Entity
@SequenceGenerator(name = "SEQ_ITENS", sequenceName = "SEQ_ITENS", initialValue = 1)
public class Itens implements Serializable {
private static final long serialVersionUID = -7243205344072660982L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ITENS")
private Integer idItens;
@OneToOne(mappedBy = "itens")
private ItensLancados itensLancados;
@ManyToOne
private Setor setor;
}
Partner :
@Entity
@SequenceGenerator(name = "SEQ_PARCEIRO", sequenceName = "SEQ_PARCEIRO", initialValue = 1)
public class Parceiro implements Serializable {
private static final long serialVersionUID = -3959545077780299993L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_PARCEIRO")
private Integer codParceiro;
@OneToMany(mappedBy = "parceiro")
private List<ItensLancados> itensLancados;
@ManyToMany(mappedBy = "parceiros")
private List<Cotacao> cotacoes;
}
Quoted Items :
@Entity
@IdClass(CotacaoParceiroId.class)
public class ItensLancados implements Serializable {
private static final long serialVersionUID = -9021268714143165841L;
@Id
@ManyToOne
@JoinColumn(name = "cotacao_id")
private Cotacao cotacao;
@Id
@OneToOne
@JoinColumn(name = "item_id")
private Itens itens;
@Id
@ManyToOne
@JoinColumn(name = "parceiro_id")
private Parceiro parceiro;
}