我有一张有 Players 的桌子,还有一张有 Games 的桌子。它们之间存在 Player----(1..n)[Game] 关系(字段定义可能不完全正确):
// 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 甚至不需要)?