I have a problem when setting a value in query.setParameter
. Using EcliseLink and MySQL, I made a query without setting parameters and it worked, what could it be?
Class where I try to query:
/*
* 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();
}
}
Generic Class:
/*
* 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;
}
}
Give this error:
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, business_id FROM employee WHERE ((email =?) AND (password =?)) bind = > [2 parameters bound] Query: ReadAllQuery (name="Official.authenticate" referenceClass = Official sql="SELECT id, email, name, password, phone, business_id FROM employee WHERE ((email =?) AND (password =?))))