簡體   English   中英

為什么我不能從查詢中獲得結果列表?

[英]Why can't I get result list, from a query?

我想僅在他們的帳戶被激活時才登錄我的用戶,但由於某種原因,當給出正確的憑據時,應該將用戶返回給我的查詢(我稱之為我的程序中的角色),根本不會檢索任何內容。

我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。 怎么了?

調試步驟,

  1. 使用任何SQL客戶端運行相同的查詢
  2. 檢查您的Role類。 它必須具有適當的getter / setter的emailpassword屬性

請不要告訴我數據庫中的密碼是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()) {...} 

您的查詢列表必須為空。 這與您連接的數據庫(可能不是您認為正在點擊的數據庫),查詢,參數或數據庫有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM