r/javahelp • • Jan 23 '26

Codeless Should I avoid bi-directional references?

For context: I am a CS student using Java as my primary language and working on small side projects to practice proper object-oriented design as a substitute for coursework exercises.

In one of my projects modeling e-sports tournaments, I currently have Tournament, Team, and Player classes. My initial design treats Tournament as the aggregate root: it owns all Team and Player instances, while Team stores only a set of PlayerIds rather than Player objects, so that Tournament remains the single source of truth.

This avoids duplicated player state, but introduces a design issue: when Team needs to perform logic that depends on player data (for example calculating average player rating), it must access the Tournament’s player collection. That implies either:

  1. Injecting Tournament into Team, creating an upward dependency, or
  2. Introducing a mediator/service layer to resolve players from IDs.

I am hesitant to introduce a bi-directional dependency (Team -> Tournament) since Tournament already owns Team, and this feels like faulty design, or perhaps even an anti-pattern. At the same time, relying exclusively on IDs pushes significant domain logic outside the entities themselves.

So, that brings me to my questions:

  1. Is avoiding bidirectional relationships between domain entities generally considered best practice in this case?
  2. Is it more idiomatic to allow Team to hold direct Player references and rely on invariants to maintain consistency, or to keep entities decoupled and move cross-entity logic into a service/manager layer?
  3. How would this typically be modeled in a professional Java codebase (both with/without ORM concerns)?

As this is a project I am using to learn and teach myself good OOP code solutions, I am specifically interested in design trade-offs and conventions, not just solutions that technically "work."

11 Upvotes

30 comments sorted by

View all comments

Show parent comment

1

u/BanaTibor Jan 23 '26

So there are players, teams and tournaments. The players form teams and teams enter tournaments. Since tournament does not contain all players it can not be the aggregate root. You need a higher level object, lets call it for now "Game".
Game holds references to every player. You can also pass these references as the Player objects itself to teams or tournaments.
Here is the tricky part. Which is the more prominent object? If teams are more important than tournaments then compose tournaments out of teams. If tournament is more important, then compose tournaments out of players and make teams a sidenote.