Search code examples
androideclipseormormlite

Query for id needs integer parameter


In the documentation the following is mentioned:

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

But in Eclipse(android) i only see the option to pass an integer as parameter? any help?


Solution

  • The Dao objects use generics to enforce the type of id is associated with your entity. If you only see the option to pass an integer into dao.queryForId(...) then you probably, mistakenly, defined the dao like:

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

    The first generic parameter specifies the type of the entity and the second generic parameter specifies the type of the ID field in that entity. With the Integer, you will call accountDao.queryForId(Integer).

    As @Tomas mentioned, you need to define your DOA with something like:

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

    Then you can query for Account by a String id:

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