You are not setting the value in the.setParameter query when trying to make a query in the database

Asked

Viewed 233 times

0

I’m having a problem when it comes to setting a value on query.setParameter. Use Ecliselink and Mysql, I made a query without setting parameters and it worked, I wonder if it can be?

Class where I try to consult:

/*
 * 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 br.com.redew.dao;

import br.com.redew.entidade.Funcionario;
import java.io.Serializable;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author Desenvolvimento
 */
public class FuncionarioDao extends GenericoDao<Funcionario, Integer> implements Serializable {

    public Funcionario autenticar(Funcionario funcionario) throws NoResultException, NonUniqueResultException {
        Query query = getEm().createNamedQuery("Funcionario.autenticar");
        query.setParameter("email", funcionario.getEmail());
        query.setParameter("senha", funcionario.getSenha());
        return (Funcionario) query.getSingleResult();
    }
}

Classe Genericodao:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.redew.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author weder
 */
public class GenericoDao<T, ID> implements Serializable {

    private EntityManagerFactory emf;
    private EntityManager em;
    private Class<T> classe;

    public GenericoDao() {
        emf = Persistence.createEntityManagerFactory("br.com.redew_redew_war_1.0.0PU");
        em = emf.createEntityManager();
        classe = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public T find(ID id) {
        return getEm().find(classe, id);
    }

    public Object insert(T Objeto) {
        getEm().getTransaction().begin();
        getEm().persist(Objeto);
        getEm().flush();
        getEm().getTransaction().commit();
        return Objeto;
    }

    public Object update(T Objeto) {
        getEm().getTransaction().begin();
        getEm().merge(Objeto);
        getEm().flush();
        getEm().getTransaction().commit();
        return Objeto;
    }

    public Object delete(T Objeto) {
        getEm().getTransaction().begin();
        getEm().remove(Objeto);
        getEm().flush();
        getEm().getTransaction().commit();
        return Objeto;
    }

    public List callNamedQuery(String namedQuery) {
        Query q = getEm().createNamedQuery(namedQuery);
        return q.getResultList();
    }

    public List callNativeNamedQuery(String query) {
        Query q = getEm().createNativeQuery(query);
        return q.getResultList();
    }

    public EntityManager getEm() {
        return em;
    }
}

Makes that mistake:

Caused by: javax.persistence.Persistenceexception: Exception [Eclipselink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.Databaseexception Internal Exception: com.mysql.jdbc.exceptions.jdbc4.Mysqlnontransientconnectionexception: No Operations allowed after Connection closed. Error Code: 0 Call: SELECT id, email, name, password, phone, id_company FROM funcio WHERE ((email = ?) AND (password = ?)) bind => [2 bound Parameters] Query: Readallquery(name="Funcionario.autenticar" referenceClass=Funcionario sql="SELECT id, email, name, password, phone, id_company FROM funcio WHERE ((email = ? ) AND (password = ?))")

  • You are trying to query with the connection closed. Check the connection before the query.

  • Before consulting I check the connection on a "Genericodao".

  • If possible, add an excerpt where you call this method to see the source of the problem. The error is clear, you are trying to query but the connection is closed.

  • I already edited the question is there the classes :)

  • You could include the class Funcionario in order to evaluate your query?

1 answer

0


I’ve decided it’s so thank you guys:

public List<Funcionario> autenticar(Funcionario funcionario) {
        Query query = getEm().createNamedQuery("Funcionario.autenticar");
        query.setParameter("email", funcionario.getEmail());
        query.setParameter("senha", funcionario.getSenha());
        return query.getResultList();
    }
  • What did you do differently? This code is virtually identical to the one that was giving the question problem.

  • 1

    I’m returning a list now 'query.getResultList();' instead of 'query.getSingleResult();'

Browser other questions tagged

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