我想僅在他們的帳戶被激活時才登錄我的用戶,但由於某種原因,當給出正確憑據時應該返回給我的用戶(我稱之爲角色在我的程序中)的查詢根本不檢索任何內容。爲什麼我無法從查詢中獲得結果列表?
我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。哪裏不對?
什麼類型是「r FROM Role」?可能你的查詢什麼都不返回 – wegginho 2011-04-26 09:32:01
您是否檢查過創建了哪個SQL語句,並且是否直接在數據庫上測試了該語句? – Thomas 2011-04-26 09:36:01
表的名稱是「角色」。返回聲明我什麼都沒有返回,但我給了很好的憑據,我不明白爲什麼它不會返回true – sfrj 2011-04-26 09:36:29