Reputation: 935
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!
Upvotes: 1
Views: 752
Reputation: 116888
Interesting. The problem is that you have limited the columns to "id"
and "name"
. ORMLite is therefore not creating your ForeignCollection
fields for you.
This was a bug was fixed in version 4.30. We've added the ability to specify the ForeignCollection
column name in selectColumns()
. Here's the bug fix around this:
https://sourceforge.net/tracker/?func=detail&aid=3428751&group_id=297653&atid=1255989
You need to modify your QueryBuilder
code to also "select" the members
field.
...selectColumns("id", "name", "members")...
Here are the docs for the selectColumns()
method:
To quote:
WARNING: If you specify any columns to return, then any foreign-collection fields will be returned as null unless their ForeignCollectionField.columnName is also in the list.
Upvotes: 2