Doubt regarding join with multiple tables

0

I have a sales class and it receives many products (manyToOne) and client (oneToMany) and would like to put everything together into a join to make a report with just the information you want.

I tried to venture to implement but gave 500 Error in web service. I have been giving a researched and I believe it is because I did not add @joinColumn but even so I do not know what the use of it

Sales Entity:

package entidade;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author 631520084
 */
@Entity
@XmlRootElement
public class Venda implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String dtCompra;
    @OneToMany
    private List<Produto>produtos;
    @ManyToOne
    private Cliente cliente ;

    public Venda() {
    }

    public Venda(String dtCompra, List<Produto> produtos, Cliente cliente) {
        this.dtCompra = dtCompra;
        this.produtos = produtos;
        this.cliente = cliente;
    }



    public String getDtCompra() {
        return dtCompra;
    }

    public void setDtCompra(String dtCompra) {
        this.dtCompra = dtCompra;
    }

    public List<Produto> getProdutos() {
        return produtos;
    }

    public void setProdutos(List<Produto> produtos) {
        this.produtos = produtos;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Venda)) {
            return false;
        }
        Venda other = (Venda) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entidade.Venda[ id=" + id + " ]";
    }

}

Sale Dao and the join I used that did not funfou

*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rn;

import entidade.Fornecedor;
import entidade.Produto;
import entidade.Venda;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import util.Conexao;

/**
 *
 * @author 631520084
 */
public class VendaRN {


    public Venda inserir(Venda venda) {
        Conexao con = new Conexao();
        EntityManager em = con.getEntidade();
        em.getTransaction().begin();
        for (Produto produto : venda.getProdutos()) {
            em.merge(produto);
        }
        em.merge(venda.getCliente());
        em.persist(venda);
        em.getTransaction().commit();
        em.close();
        return venda;
    }
    public List<Venda> relatorio() {
        Conexao con = new Conexao();
        EntityManager em = con.getEntidade();
        String jpql = "SELECT p.nome,c.nome, p.preco, v.dtCompra FROM Venda v JOIN v.cliente c JOIN v.produtos p";
        Query query = em.createQuery(jpql);
        List<Venda> listaVendas = query.getResultList();

        em.close();
        return (listaVendas);

    }

}

Error

  

HTTP Status 500 - Internal Server Error Type Status Report

     

Message Internal Server Error

     

Description The server encountered an unexpected condition that   prevented it from fulfilling the request.

     

Apache Tomcat / 8.5.30

NOTE: The insert method works so it is not a problem in the webservice, the join is compiling.

    
asked by anonymous 28.04.2018 / 17:58

1 answer

1

@JoinColumn is for you to inform the column of the table that references another table. For example: if you have the table Sale, this table should have a column where you inform the client (clientId for example), then you would inform:

@ManyToOne
@JoinColumn(name="clienteId")
private Cliente cliente ;

In the case of OneToMany, you usually inform the column in the referenced table, and then inform the entity, then in Product you would have:

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

And on Sale:

@OneToMany(mappedBy="venda") //aqui eu informei a propriedade na entidade Produto onde eu referenciei Venda
private List<Produto> produtos;

About this error, this is a generic tomcat error. To know the exact error in your application you should see the exception message on the terminal or inside the IDE that you use, this message tells you more precisely what the error in your code is.

    
28.04.2018 / 19:46