Java 为什么我不能从查询中获取结果列表?

Java 为什么我不能从查询中获取结果列表?,java,jpa,jakarta-ee,java-ee-6,Java,Jpa,Jakarta Ee,Java Ee 6,我只想在用户帐户被激活的情况下登录用户,但由于某些原因,当获得正确的凭据时,应该返回我在程序中称之为角色的用户的查询根本无法检索任何内容 我100%确信凭据是正确的,而且我确信数据库中没有重复项,并且查询语法是正确的 那么为什么我的一个EJB中的这个方法不返回true呢 // Checks if the account is not activated! public boolean isActivated(String email, String password) {

我只想在用户帐户被激活的情况下登录用户,但由于某些原因,当获得正确的凭据时,应该返回我在程序中称之为角色的用户的查询根本无法检索任何内容

我100%确信凭据是正确的,而且我确信数据库中没有重复项,并且查询语法是正确的

那么为什么我的一个EJB中的这个方法不返回true呢

// Checks if the account is not activated!
    public boolean isActivated(String email, String password) {
        // 1-Send query to database to see if that user exist
        Query query = em
                .createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
        query.setParameter("emailparam", email);
        query.setParameter("passwordparam", password);

        List<Object> tmpList = query.getResultList();
        if (tmpList.isEmpty() == false) {
            System.out.println("IS NOT EMPTY");
            Role role = (Role) tmpList.get(0);
            if (role.getAccountStatus().trim()
                    .equalsIgnoreCase(AccountStattus.ACTIVATED.toString())) {
                // The account is activated!
                System.out.println("ACTIVATED!!!!!!!");
                return true;
            }
        }
        // The account is not activated!
        System.out.println("NOT ACTIVATED!!!!!!!");
        return false;
    }
我在控制台中看到的唯一消息未激活

更新

为了确认列表100%为空,我修改了代码如下:

// Checks if the account is not activated!
    public boolean isActivated(String email, String password) {
        // 1-Send query to database to see if that user exist
        System.out.println("HERE:" + email+password);
        Query query = em
                .createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
        query.setParameter("emailparam", email);
        query.setParameter("passwordparam", password);

        List<Object> tmpList = query.getResultList();
        Iterator<Object> it = tmpList.iterator();
        int elements = 0;
        while(it.hasNext()) { 
               elements++;
             }


        if (elements > 0) {
            System.out.println("IS NOT EMPTY");
            Role role = (Role) tmpList.get(0);
            if (role.getAccountStatus().trim()
                    .equalsIgnoreCase(AccountStattus.ACTIVATED.toString())) {
                // The account is activated!
                System.out.println("ACTIVATED!!!!!!!");
                return true;
            }
        }
        // The account is not activated!
        System.out.println("NOT ACTIVATED!!!!!!!");
        return false;
    }

当我输入正确的凭据时,该方法仍然返回false。怎么了?

您的查询列表必须为空。这要么是您所连接的数据库(可能不是您认为正在点击的数据库)、查询、参数或数据库的问题

调试步骤

使用任何SQL客户机运行相同的查询 检查你的角色类别。它必须具有具有适当getter/setter的电子邮件和密码属性 请不要告诉我数据库中的密码是散列版本

一些基本的重构建议

用这种方式写下你的问题,让你的眼睛看得更清楚

Query query = em.createQuery("from Role r where r.email=:email and r.password=:password")
                .setParameter("email", email)
                .setParameter("password", password);
把你的情况改成这样

if (!list.isEmpty()) {...}

角色的r是什么类型的?您的查询可能不会返回任何结果吗?您是否检查了创建的SQL语句,并且是否直接在DB上测试了该SQL语句?表的名称为Role。return语句没有返回任何内容,但我提供了良好的凭据,我不明白它为什么没有返回true@TheElite Gentelman我照你说的做了,但这是控制台说的:语法错误解析查询[SELECT*FROM Role r其中r.email=:emailparam和r.password=:passwordparam],第1行第7列:意外令牌[*]@sfrj,对不起,我弄错了。我混淆了SQL和JPQL。我删除了我的评论。是的,列表是空的,这是肯定的,因为如果tmpList.isEmpty==false的条件没有计算为true。我100%确信数据库是正确的,因为我可以正确注册用户,并且可以看到数据库中插入的数据。看起来密码参数在途中丢失了。查询很好,但到达EJB时凭据错误。谢谢大家!@很高兴它成功了。但是为什么密码参数丢失了,而不是电子邮件?只是好奇,没什么特别的。我使用一个托管bean从前端获取密码并进行处理。我做了一些字符串操作来删除空格,但最后没有修剪。所以密码是错的。我看不到它没有在控制台中被修剪,因为我看不到单词末尾的空格:我有点傻,可能是昨天我重构时造成的问题。