2017-02-14 55 views
1

额外的字段我有3个表:多到许多activejdbc

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

和连接表

play(person_id, game_id, score). 

随着ActiveJdbc我用many2many注释并能正常工作以获取所有游戏1人

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

我的问题是如何获得值“分数”? 我试过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; 

回答

1

有可能是这样做的不止一种方法,但这里是一个方式。您可以将多对多关系视为两个一对多关系,其中,Play是Person和Game两者的孩子。然后你从不同的角度来处理:

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

使用include()你也优化和运行更少的查询:http://javalite.io/lazy_and_eager

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

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

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

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

希望它能帮助!

+0

好的,感谢编辑我的文章。为了你的回答,我真的不明白。我想要的是显示1个给定人的戏剧列表+得分。我编辑了我的帖子。但是从你的答案来看,我必须从“游戏”中获得分数,但在我的模型中(“游戏”),我没有“getScore”方法,这导致我得到:在com类型上找不到“财产”分数.myapp.models.Game。当我在jsp页面 – Rony

+0

中使用它时,我的代码中有一个拼写错误,只是修复了。由于您在Play上获得分数,因此您可以从该对象中获取分数。 – ipolevoy

+0

啊好的!得到它了!现在的作品,我用包括像你说的:)谢谢很多! – Rony