5
I’m doing a project with JPA regarding a quotation system and I’m having doubts on how to build the relationship between classes, and would like (if possible) some opinions.
My project is a quote system, where the company will create a quote and will 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: will contain the register of sectors;
- Items: will contain the registration of the items that are attached to the sectors;
- Entries: will contain the release of quantities and values entered by partners.
My biggest doubt now is on how to build the relationship between them. I tried to build a relationship this way:
Cotacaowill contain a list of the entityParceirowith@OneToMany(a quote may have one or more partners);Cotacaowill contain a list of the entitySetorwith@OneToMany(a quote may have one or more sectors);Setorwill contain a list of the entityItenswith@OneToMany(a sector may have one or more items);Parceirowill contain a list of the entityItensLancados(a partner may have one or more items launched).
When I run the project and see how the BD was created I can not find relationship in the tables between the item launched with the item, nor between the item launched with the quotation that the partner typed.
Would anyone have any tips on how to build this relationship?
Follow the classes for analysis.
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;
}
Points:
@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;
}
Hello quikkoo, I edited the statement with the codes of the classes and their relationships if you want to take a look. Thank you for your attention.
– Ismael Junior