Eu estou recebendo esse erro ai abaixo quando eu tento passar o id de cliente ao list de produto
@ManyToMany(mappedBy="prod")
private List<Cliente>cli;
java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.pizzaria.pizzariaweb.model.Produto (java.util.ArrayList is in module java.base of loader 'bootstrap'; com.pizzaria.pizzariaweb.model.Produto is in unnamed module of loader org.glassfish.web.loader.WebappClassLoader @33465a2f)
Pedido
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" template="../WEB-INF/template/layout.xhtml">
<ui:define name="content">
<main class="container mt-5 text-center">
<p:inputText value=" #{produtoBean.dados()}" type="hidden"/>
<h:form>
<p:dataTable var="produto" value="#{produtoBean.listprodutos}" selection="#{produtoBean.produto}" selectionMode="single" rowKey="#{produto.id}">
<p:column headerText="Id">
<h:outputText value="Id: #{produto.id} " class="produtoText" />
</p:column>
<p:column headerText="Sabor">
<h:outputText value="Sabor: #{produto.sabor} " class="produtoText" />
</p:column>
<p:column headerText="Tamanho">
<h:outputText value="Tamanho: #{produto.tamanho} " class="produtoText" />
</p:column>
<p:column headerText="Valor">
<h:outputText value="Valor: #{produto.valor}" class="produtoText" /><br/>
</p:column>
</p:dataTable>
<h:commandButton value="Salvar" class="btn btn-primary btn-lg" action="#{produtoBean.save()}" />
<h:link value="Voltar" outcome="menu" class="btn btn-primary btn-lg ms-3" />
</h:form>
</main>
</ui:define>
</ui:composition>
ProdutoBean
@Named
@ViewScoped
public class ProdutoBean implements Serializable {
private Produto produto = new Produto();
Cliente cliente = new Cliente();
List<Cliente> listcliente = new ArrayList();
List<Produto> listprodutos = new ArrayList();
private DaoGeneric<Produto> daoGeneric = new DaoGeneric();
public List<Produto> dados() {
listprodutos = daoGeneric.findAll();
return listprodutos;
}
public String save() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
Long id = (Long) session.getAttribute("id");
cliente.setId(id);
listcliente.add(cliente);
listprodutos.add((Produto) listcliente);
daoGeneric.salvarProduto(listprodutos);
return "menu?faces-redirect=true";
}
public Produto getProduto() {
return produto;
}
public void setProduto(Produto produto) {
this.produto = produto;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public List<Cliente> getListcliente() {
return listcliente;
}
public void setListcliente(List<Cliente> listcliente) {
this.listcliente = listcliente;
}
public List<Produto> getListprodutos() {
return listprodutos;
}
public void setListprodutos(List<Produto> listprodutos) {
this.listprodutos = listprodutos;
}
}
DaoGeneric
public class DaoGeneric<E> {
public void salvar(E entidade) {
EntityManager entityManager = new JPAUtil().getEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
entityManager.persist(entidade);
entityTransaction.commit();
entityManager.close();
}
public E merge(E entidade) {
EntityManager entityManager = new JPAUtil().getEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
E retorno = entityManager.merge(entidade);
entityTransaction.commit();
entityManager.close();
return retorno;
}
public List<E> findAll() {
EntityManager entityManager = new JPAUtil().getEntityManager();
ArrayList<E> entidade = null;
try {
entidade = (ArrayList<E>) entityManager.createQuery("from Produto").getResultList();
} catch (NoResultException e) {
System.err.println(e);
} finally {
entityManager.close();
}
return entidade;
}
public E findOne(Long id) {
EntityManager entityManager = new JPAUtil().getEntityManager();
E con = null;
try {
con = (E) entityManager.createQuery("from Cliente where id=:id").setParameter("id", id).getSingleResult();
} catch (NoResultException e) {
System.err.println(e);
} finally {
entityManager.close();
}
return con;
}
public E login(String nome, String senha) {
EntityManager entityManager = new JPAUtil().getEntityManager();
E log = null;
try {
log = (E) entityManager.createQuery("from Cliente where nome=:nome and senha=:senha").setParameter("nome", nome).setParameter("senha", senha).getSingleResult();
} catch (NoResultException e) {
System.out.println(e);
} finally {
entityManager.close();
}
return log;
}
public void delete(E entidade) {
EntityManager entityManager = new JPAUtil().getEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
entityManager.remove(entidade);
entityTransaction.commit();
entityManager.close();
}
public void deletePorId(E entidade) {
EntityManager entityManager = new JPAUtil().getEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
Object id = JPAUtil.getPrimaryKey(entidade);
entityManager.createQuery("delete from " + entidade.getClass().getCanonicalName() + " where id = " + id).executeUpdate();
entityTransaction.commit();
entityManager.close();
}
public void salvarProduto(List<E> entidade) {
EntityManager entityManager = new JPAUtil().getEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
entityManager.merge(entidade);
entityTransaction.commit();
entityManager.close();
}
}
Cliente
@Entity
public class Cliente implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String senha;
private String cpf;
private String endereco;
private String cep;
private String tel;
@ManyToMany
private List<Produto> prod;
public Cliente() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public List<Produto> getProd() {
return prod;
}
public void setProd(List<Produto> prod) {
this.prod = prod;
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Cliente other = (Cliente) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Cliente{" + "id=" + id + ", nome=" + nome + ", senha=" + senha + ", cpf=" + cpf + ", endereco=" + endereco + ", cep=" + cep + ", tel=" + tel + ", prod=" + prod + '}';
}
}
Produto
@Entity
public class Produto implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String sabor;
private String tamanho;
private String valor;
@ManyToMany(mappedBy="prod")
private List<Cliente>cli;
public Produto() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSabor() {
return sabor;
}
public void setSabor(String sabor) {
this.sabor = sabor;
}
public String getTamanho() {
return tamanho;
}
public void setTamanho(String tamanho) {
this.tamanho = tamanho;
}
public String getValor() {
return valor;
}
public void setValor(String valor) {
this.valor = valor;
}
public List<Cliente> getCli() {
return cli;
}
public void setCli(List<Cliente> cli) {
this.cli = cli;
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Produto other = (Produto) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Produto{" + "id=" + id + ", sabor=" + sabor + ", tamanho=" + tamanho + ", valor=" + valor + ", cli=" + cli + '}';
}
}