I have a namedQuery that for some reason is returning null results from the database.
The namedQuery itself:
@NamedQuery(name="Usuario.userLogin", query = "SELECT u.email, u.senha FROM Usuario u WHERE u.email = :mail AND u.senha = :senha")
The password is encrypted using md5 (not much use, but it is an academic work).
Implementing password decryption:
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
String mail = request.getParameter("mail");
String senha = request.getParameter("senha");
try {
senha = ControllerUsuario.criptografa(senha);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(CadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
}
Usuario user = ControllerUsuario.getSenha(mail, senha);
if (user != null) {
HttpSession session = request.getSession();
pw.println("Tipo: " + user.getEmail());
session.setAttribute("mail", user.getEmail());
session.setAttribute("tipo_usr", user.getTipo_usr());
session.setAttribute("usr", user.getUsuario());
session.setAttribute("logado", 1);
// response.sendRedirect("index");
} else {
response.sendRedirect("cadastro-falha.html");
}
}
Implementation of getSenha
public static Usuario getSenha(String mail, String senha) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = null;
Transaction tx = null;
Usuario user = new Usuario();
try {
session = sf.openSession();
tx = session.getTransaction();
tx.begin();
Query query = session.getNamedQuery("Usuario.userLogin");
query.setParameter("mail", mail);
query.setParameter("senha", senha);
user = (Usuario) query.uniqueResult();
tx.commit();
} catch (Exception e) {
if (tx != null) tx.rollback();
} finally {
session.close();
}
return user;
}
What happens?