1

我有一张有 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 甚至不需要)?

4

1 回答 1

0

我的猜测是 ormLite 会为每个玩家做一个查询。与一个 daoGames.getAllGroupedByName() 相比,这是否是一个很大的开销(尽管 groupBy 甚至不需要)?

这是对的。ORMLite 不能通过对每个Player结果进行单独查询来非常有效地做到这一点。您getAll()手动完成并匹配它们的机制很可能会更快。

ArrayList除非你真的需要LinkedList能力,否则我会使用。

于 2014-09-04T15:58:54.747 回答