0
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’s academic work).
Implementation of 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?
Take the generated SQL, the parameters and execute directly on an SQL client. What is the result?
– Bruno César
@Brunocésar gave exactly what he was supposed to give. hash is also coming correctly, but for some reason Hibernate is not saving the attributes of the object in the
user
. maybe I’m using it wrong, but I don’t know what it is– athosbr99