0

I have a fairly simple model with two objects: Team and Game. The Team object has the name of the team, and the Game object looks-up to the Team object twice. One through the "away_team_id" and one through the "home_team_id" field.

  Game ----home_team_id----> Team
    |
    +------away_team_id----> Team

I'm fairly new to ActiveJDBC, but have used PHP ActiveRecord for quite some time. I cannot for the life of me figure out how to reference each team through the Game object. I do have these annotations in my Game object:

@BelongsToParents({ 
@BelongsTo(foreignKeyName="home_team_id",parent=Team.class), 
@BelongsTo(foreignKeyName="away_team_id",parent=Team.class)
}) 

public class Game extends Model {
}

In my test code, I have:

Team homeTeam = game.parent (Team.class);

But obviously that's only one of them, and I'm not even sure how it figures out which one it is! Any help would be greatly appreciated.

Thanks in advance!

Dave
  • 2,546
  • 6
  • 23
  • 29

1 Answers1

1

This is an unusual case. I would suggest something like this:

public class Game extends Model{
  public Team getHomeTeam(){
    return Team.findFirst("id = ?", get("home_team_id"));
  }
    public Team getAwayTeam(){
    return Team.findFirst("id = ?", get("home_away_id"));
  }
}

I would suggest wrapping ActiveJDBC in semantic methods like this to help potential refactoring in the future.

tx

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Okie dokie. I really "think" my model is the best way to implement what I'm trying to do. I was able to make it work easily with PHP Active Record, but when I tried working in Java, that's when I hit the issue. – Dave Oct 02 '13 at 21:53
  • That's not an "unusual case" at all. The semantic methods make sense, but you lose the eager loading feature. It would be possible to make the include() method to specify what "field" you are including? – mppfiles May 09 '16 at 11:34
  • I mean, parent() or include() methods shoud specify what "field" you want, not just the class name. – mppfiles May 09 '16 at 11:41
  • here is the link to the Github issue and a discussion: https://github.com/javalite/javalite/issues/1087 – ipolevoy Mar 29 '21 at 17:41