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

时间:2011-04-26 09:23:28

标签: java jpa java-ee java-ee-6

我想仅在他们的帐户被激活时才登录我的用户,但由于某种原因,在给出正确的凭据时,应该返回给我的用户的查询(我在程序中将其称为Role),根本不会检索任何内容。

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

我在控制台中看到的唯一消息是 NOT ACTIVATED !!!!

更新

要确认列表为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。有什么问题?

2 个答案:

答案 0 :(得分:2)

调试步骤,

  1. 使用任何SQL客户端运行相同的查询
  2. 检查您的Role课程。它必须具有emailpassword属性以及适当的getter / setters
  3. 请不要告诉我数据库中的密码是hashed-version

    很少有基本的重构建议,

    1. 以这种方式编写查询,以方便眼睛,

      Query query = em.createQuery("from Role r where r.email=:email and r.password=:password")
                      .setParameter("email", email)
                      .setParameter("password", password);
      
    2. 将您的状况改为此

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

答案 1 :(得分:0)

您的查询列表必须为空。这与您连接的数据库(可能不是您认为正在点击的数据库),查询,参数或数据库有关。