I tried usign OrmLite in an android Application, and encountered a problem:
I created a foreign collection on an object, and assigned it some objects. I can use the queryForAll() Method on the DAO to query the objects, and it returns them as expected:
allTeams = getHelper().getTeamDao().queryForAll();
for(ScrumTeam team : allTeams) {
if(team.getMembers() == null || team.getMembers().size() == 0) {
log("No Members");
} else {
log(team.getMembers().size()+" Members");
}
}
This works as expected, printing "2 Members" or "3 Members" on the Teams, representing the correct data.
Obviously, I wanted to sort the data, and the method to do this I found was using the queryBuilder:
allTeams = getHelper().getTeamDao().queryBuilder().selectColumns("id", "name").orderBy("name", true).query();
for(ScrumTeam team : allTeams) {
if(team.getMembers() == null || team.getMembers().size() == 0) {
log("No Members");
} else {
log(team.getMembers().size()+" Members");
}
}
The above code outputs "No members", but i can access other data of the team (id and name) perfectly. I tried setting eager to true and false on the collection, but neither worked. Investigating further, I found out, that the members array is not initialized (null). Is this a bug, or am I using the Library incorrectly? Is there a way to fetch the ForeigCollection manually?
Thanks for your replies!
Interesting. The problem is that you have limited the columns to
"id"and"name". ORMLite is therefore not creating yourForeignCollectionfields for you.This was a bug was fixed in version 4.30. We've added the ability to specify the
ForeignCollectioncolumn name inselectColumns(). Here's the bug fix around this:You need to modify your
QueryBuildercode to also "select" themembersfield.Here are the docs for the
selectColumns()method:To quote: