Questions in relationships Ibernate

Asked

Viewed 28 times

0

Could someone ask me some questions about Ibernate? Good have 2 product and sales tables with a n:n relationship that generated an item_sale table

I did it in my two models to map:

Class Products:

@Entity(name="Produtos")
@Table(name = "produtos")
public class Produtos {
    @Id
    @GeneratedValue
    private Long codProduto;
    @Column(name="preco_Venda")
    private Double preco_Venda;
    @Column(name="nome")
    private String nome;    
}

the class Sale:

@Entity(name="Venda")
@Table(name = "venda")
public class Venda {
    @Id
    @GeneratedValue
    private Long codVenda;
    @Column(name="data_venda")
    private Date dataVenda;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "idCaixa")
    private Caixa caixa;
    @ManyToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name="produtos_idVenda", referencedColumnName="idVenda",nullable=false)
    private List<Lista_Produtos> listaProdutos;
}

good i don’t need to have this my item_sell table as a class

one of my doubts is, with this mapping what I would need to insert a value in this item_sale table.

Another question is regarding my Products class:

Would I need a Sale composition? ex:

Private sale;

//

or I would need to create + an item_Venda class

// to illustrate my relationship in the database:

inserir a descrição da imagem aqui

1 answer

0


In this case you have no escape from having the table Itemvenda as an entity in your project, precisely because of its structure. If it were only a simple table of relation between Product and Sale then it would be possible because the Hibernate would know how to include the necessary data (the fks of the related tables).

Anyway, in the situation you have, it really is a N-N amid Produto-Venda, however is a 1-N amid Produto-Itemvenda and Venda-Itemvenda. Then you must have in your entity Sale one @OneToMany for Itemvenda (and also map the inverse in Itemvenda as @ManyToOne)

Example of mapping:

@Entity
class Venda {
  /* outros fields omitidos... */

  @OneToMany(mappedBy="venda")
  private List<ItemVenda> items;
}

@Entity
class ItemVenda {
 /* omitindo outros campos.. */

  @ManyToOne
  @JoinColumn(name="id_venda")
  private Venda venda;

  @ManyToOne
  @JoinColumn(name="id_produto")
  private Produto produto;
}

@Entity
class Produto {
  /* omitindo outros campos.. */ 

  // Este mapeamento é opcional, coloca se fizer sentido pra o seu negócio..
  @OneToMany(mappedBy="produto")
  private List<ItemVenda> vendas;

}


An example of how you could assemble this data to persist would look something like this:

Caixa caixa = new Caixa(1);
Produto produto = new Produto(); /* omitindo inicialização do produto */


Venda venda = new Venda();
venda.setDataVenda(new Date());
venda.setCaixa(caixa);

venda.addItem(new ItemVenda(produto, 2 /* quantidade */));

/* salvar entidade com hibernate.. */

Browser other questions tagged

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