Doubt regarding Join with multiple tables

Asked

Viewed 38 times

0

I have a sale class and the same receives several products(manyToOne) and customer (oneToMany) and would like to join all in a Join to make a report with only the desired information.

I tried to venture to implement but gave Error 500 on web service. I’ve been doing some research and I believe it’s because I didn’t add @joinColumn but I still don’t know what the use of it is

Sale 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 + " ]";
    }

}

From the sale and the Join I used that didn’t function

*
 * 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 there is no problem in the webservice, the Join is compiling.

1 answer

1

The @Joincolumn is for you to inform the column of table q references another table. For example: if you have the Sale table, this table should have a column where you inform the customer (clienteId for example), then vc would inform:

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

In the case of Onetomany, normally you 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 error of Tomcat. To know the exact error in your application you should see the exception message in the terminal or within the IDE you use, this message tells you more precisely which error in your code.

Browser other questions tagged

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