Data modeling via JPA and Hibernate

Asked

Viewed 372 times

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:

  • Cotacao will contain a list of the entity Parceiro with @OneToMany (a quote may have one or more partners);
  • Cotacao will contain a list of the entity Setor with @OneToMany (a quote may have one or more sectors);
  • Setor will contain a list of the entity Itens with @OneToMany (a sector may have one or more items);
  • Parceiro will contain a list of the entity ItensLancados (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;

}

1 answer

1

your mapping may be wrong or your modeling is going in a different way than what you say you want, it would be important you post the code of what you did so you can iedntify in which you may have been wrong

but for now you can follow this tutorial Hibernate - One-to-Many example (Annotation) is simple and practical, and you can use it to compare to what you have already done

  • 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.

Browser other questions tagged

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