activejdbc 中的多对多额外字段

Many-to-Many extra field in activejdbc

我有 3 个 table:

person (person_id, person_name), 
game (game_id, game_name) 

和链接的 table

play(person_id, game_id, score).

对于 ActiveJdbc,我使用了 many2many 注释,它可以很好地获得 1 人的所有游戏

List<Game> games = person.get(Game.class, "person_id = ?", personId);

我的问题是如何获取值"score"? 我尝试了 game.getScore() 但显然没有用

编辑: 我想要 1 个人的完整游戏列表。这是我想要的结果

game1 | 21
game2 | 33
game3 | 44

在 SQL 中应该是这样的:

select game.name, play.score
from game join play on (game.game_id = play.game_id)
join person on (game.person_id = person.person_id)
where person_id = 1;

可能有不止一种方法可以做到这一点,但这里是 一种 方法。您可以将 Many-to-Many 关系视为两个 One-to-Many 关系,其中 Play 是 Person 和 Game 的子关系。然后你换个角度接近:

List<Play> plays = Play.where("person_id = ?", 1).include(Person.class, Game.class); 

通过使用 include(),您还优化了 运行 更少的查询:http://javalite.io/lazy_and_eager

根据您的查询条件,您可能只有一个play:

Game game = plays.get(0).parent(Game.class);
Person = plays.get(0).parent(Person.class);

如果当然,您将能够得到您的分数:

int score = plays.get(0).getScore();

希望对您有所帮助!