我有一张桌子有玩家,另一张桌子上有游戏。它们之间存在玩家----(1..n)[游戏]关系(字段定义可能不完全正确):
// 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;
我想获取并返回所有游戏的列表。让ormLite选择ForeignCollection的开销是什么时候?还是这样做更好:
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);
}
我猜ormLite会对每个玩家进行查询。与一个daoGames.getAllGroupedByName()相比,这是一个很大的开销吗(尽管甚至不需要groupBy)?