【发布时间】:2014-09-04 14:08:21
【问题描述】:
我有一张玩家桌和另一张游戏桌。它们之间存在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 甚至不需要)?
【问题讨论】:
标签: performance ormlite foreign-collection