r/javahelp • u/Star_Dude10 • 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:
- Injecting
TournamentintoTeam, creating an upward dependency, or - 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:
- Is avoiding bidirectional relationships between domain entities generally considered best practice in this case?
- Is it more idiomatic to allow
Teamto hold directPlayerreferences and rely on invariants to maintain consistency, or to keep entities decoupled and move cross-entity logic into a service/manager layer? - 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."
View all comments
Show parent comment
0
u/okayifimust Jan 23 '26
But you need functionality for that, everywhere. That's just asking for stuff to break in unexpected places.
YOU. FUNDAMENTALLY. MISUNDERSTAND. HOW. OBJECTS. WORK.
You're confidentially wrong, where you could just listen to the people you asked for advice and be right.
each object is stored precisely once. If you put one object into multiple places, they will store nothing but the reference. You are just mimicking what the language already does for you, and I promise the people who make Java did a much better job of it than you or I could ever hope to do.