2013-03-21 54 views

回答

10

Dao對象使用泛型執行與您的實體相關聯的ID類型。如果你只看到選項整數關口進入dao.queryForId(...)那麼你很可能,錯誤,這樣定義的道:

Dao<Account, Integer> accountDao = getDao(Account.class); 

第一個泛型參數指定實體和第二泛型參數的類型指定的類型該實體中的ID字段。使用Integer,您將撥打accountDao.queryForId(Integer)

正如@Tomas提到的,你需要的東西定義你的DOA,如:

Dao<Account, String> accountDao = getDao(Account.class); 

然後,你可以通過一個String ID爲Account查詢:

Account account = accountDao.queryForId("John Smith"); 
+0

謝謝!這解決了我的問題。 – Eli 2013-03-29 14:29:47

3

首先,你應該定義實體ID爲String類型的內容:

@DatabaseTable() 
public class Account { 

    @DatabaseField(id = true) 
    private String mFullName; 
    ... 
} 

那麼你應該根據實體類型的螞蟻其ID類型聲明DAO對象。現在你可以使用queryForId ID爲String類型:

Dao<Account, String> accountDao = getAccountDao(); 
Account account = accountDao.queryForId("John Smith"); 
if (account == null) { 
    // the name "John Smith" does not match any rows 
}