Get field with jpa

1

I am working with jpa and to obtain this case a user through the nickname I use the following code:

@Override
public User obtenerPorNick(String nickName) {
    return entityManager.find(User.class, nickName);
}

That works. I understand that find() is for primarykey, What I want is to obtain a user but for another attribute, for example mail that is Unique. What should I use ???

    
asked by Pablo 12.11.2017 в 19:40
source

1 answer

1

For that you must create a method in EJB corresponds.

Example:

Public usuario buscarPorMail (String correo)
{ 
String consulta ; 
Usuario usuario=null;
Try {
Consulta="from usuario u where u.correo=?";
Query query = query. CreateQuery (consulta);
Query.setParam (1,correo);
usuario=query.getSingleResult ();
}catch ()
{}
Return usuario;
}
    
answered by 13.11.2017 / 02:12
source