1

我正在使用 ninja 框架,它利用 JPA 来访问数据库。

我有这个问题:在此处输入图像描述

我的代码:

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;
  }
}

我不知道如何解决这个问题。

我希望你能帮助我,并提前谢谢你。

4

1 回答 1

0

getSingleResult() 抛出 EntityNotFoundException,因为它没有找到具有提供参数的实体。

您在调试器中看到的问题是内部类没有 toString 方法。但这没关系。问题是它没有找到实体。

如果您的查询结果可能为空,您必须捕获 EntityNotFoundException 或调用 getResultList() 并检查它是否为空。

于 2017-11-24T12:01:31.237 回答