I have a table with Players and an other table with Games. There is Player----(1..n)[Game] relationship between them (field definitions may not be entirely correct):
// Player
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
public String name;
@ForeignCollectionField(
eager = true,
maxEagerLevel = 3)
public ForeignCollection<Game> games;
// Game
@DatabaseField
String title;
@DatabaseField
String playerName;
I would like to get and return a list of all games. When is the overhead letting ormLite do the select for the ForeignCollection? Or would it be better to do something like this:
final List<Game> allGames = daoGames.getAllGroupedByName();
final List<Player> allPlayers = gameDao.getAll();
final HashMap<String, List<Game>> games = new HashMap<String, List<Game>>();
for (Game currentGame : allGames) {
final String player = currentGame.playerName;
if (games.get(player) == null) {
games.put(player, new ArrayList<Game>());
}
final List<Game> gamesOfPlayer = games.get(player);
gamesOfPlayer.add(currentGame);
}
for (Player player : allPlayers) {
player.games = games.get(player.name);
}
My guess is ormLite would do a query for each and every player. Is it a big overhead compared to the one daoGames.getAllGroupedByName() (though the groupBy is not even needed)?
This is correct. ORMLite does not do this very efficiently with a separate query for each
Player
result. Your mechanism to do thegetAll()
and match them up by hand would most likely be faster.I'd use an
ArrayList
unless you really needLinkedList
capabilities.