2011-04-26 119 views
0

我想僅在他們的帳戶被激活時才登錄我的用戶,但由於某種原因,當給出正確憑據時應該返回給我的用戶(我稱之爲角色在我的程序中)的查詢根本不檢索任何內容。爲什麼我無法從查詢中獲得結果列表?

我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 !!!!

更新

要comfirm列表是在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。哪裏不對?

+0

什麼類型是「r FROM Role」?可能你的查詢什麼都不返回 – wegginho 2011-04-26 09:32:01

+0

您是否檢查過創建了哪個SQL語句,並且是否直接在數據庫上測試了該語句? – Thomas 2011-04-26 09:36:01

+0

表的名稱是「角色」。返回聲明我什麼都沒有返回,但我給了很好的憑據,我不明白爲什麼它不會返回true – sfrj 2011-04-26 09:36:29

回答

2

步驟來調試,

  1. 運行使用任何SQL客戶端
  2. 檢查Role類相同的查詢。它必須有一個emailpassword性質與相應的getter/setter方法

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

經過一番調試,我解決了我的問題。它看起來像密碼參數迷路了。查詢很好,但到達EJB時憑證錯誤。謝謝! – sfrj 2011-04-26 10:20:44

+1

@sfrj:很高興工作。但爲什麼'password'參數丟失了,而不是'email'?只是好奇。 – 2011-04-26 10:23:48

+1

沒什麼特別的。我使用託管bean從前端獲取密碼並處理它。我做了一些字符串操作來刪除空格,但是我沒有在最後修剪()。所以密碼錯了。我看不到它在控制檯中沒有被修剪,因爲我看不到空格在結尾的話:)有點愚蠢,從我,可能當我昨天重構我創建了這個問題。 – sfrj 2011-04-26 11:07:03

0

您的查詢清單必須爲空。這可能是您連接到的數據庫(可能不是您認爲正在竊聽的那個數據庫),查詢,參數或數據庫的問題。

+0

是的列表是空的,肯定的,因爲條件'if(tmpList.isEmpty()== false)'沒有被評估爲真。我確信100%的數據庫是正確的,因爲我可以正確註冊用戶,並且看到數據庫中插入的數據。 – sfrj 2011-04-26 09:42:35

+0

看起來你已經解決了你的問題。 – duffymo 2011-04-26 11:53:51

相關問題