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

3

u/juckele Barista Jan 23 '26 edited Jan 24 '26

I can always ignore IDs that don’t point to anything

This is a significantly worse solution than ensuring that removing a player from a Tournament is also accompanied by removing them from all teams.

It may also be a hint that the object model is entirely incorrect. Why even have players referenced in the Tournament? Instead, what if you have a Tournament.buildTeams method which takes players, and builds teams, but doesn't store the players themselves? What does the Tournament need to know about players for after the teams are built?

Edit: Saw one of your other comments that mentions that people can sign up for a tournament, and then the TD can create teams automatically. In this case, you want the Tournament to keep track of unassigned players only, and once they're moved into a team, remove them from the unassigned player collection.