I'm using the ninja framework, which utilizes JPA to access a database.
I have this problem:enter image description here
My code:
public class UtenteDao extends AbstractDao {
@Inject
Provider<EntityManager> entityManagerProvider;
@UnitOfWork
public boolean utenteValido(String user, String password) {
if (user != null && password != null ) {
EntityManager entityManager = entityManagerProvider.get();
TypedQuery<Utente> q = entityManager
.createQuery("SELECT x FROM Utente x WHERE x.user = :user AND x.password = :password AND x.attivo ='true'", Utente.class)
.setParameter("user",user)
.setParameter("password",password);
Utente utente = getSingleResult(q);
if (utente != null) {
if (utente.password.equals(password)) {
return true;
}
}
}
return false;
}
}
I don't know how to resolve this.
I hope you can help me and thank you in advance.
getSingleResult() throws an EntityNotFoundException because it didn't find an entity with the provided parameters.
The problem that you see in the debugger is that an inner class does not have a toString method. But that doesn't matter. The problem is that it didn't find an entity.
If the result of your query can be null you must either catch the EntityNotFoundException or call getResultList() and check if it's empty.