1

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.

SoloWing
  • 11
  • 1
  • 3

1 Answers1

0

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.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • I do this by doing the result list public T getSingleResult(TypedQuery query) { query.setMaxResults(1); List lista = query.getResultList(); if(lista == null || lista.isEmpty()) { return null; } return lista.get(0); } in fact if you can see by extending the abstractDao class created by me – SoloWing Nov 27 '17 at 08:38
  • What is Anagrafica? From the stacktrace I see that the execption is thrown in the toString method of this class – Simon Martinelli Apr 22 '21 at 12:16