Unidirectional vs Bidirectional JPA Relationships
Fast recall
Quick revision
Core answer
Whether you understand navigation direction, how mappedBy assigns FK ownership, and the JSON serialization risk of bidirectional graphs
Making every relationship bidirectional by default, causing circular references in JSON and needless synchronization code
Show awareness of the infinite-loop problem and name the fixes: @JsonManagedReference/@JsonBackReference, @JsonIgnore, or DTOs
Interview-ready guide
Interview answer
The difference comes down to navigation direction. In a unidirectional relationship only one entity has a reference to the other. For example, Order has a customer field, but Customer doesn't have an orders list. You can go from an order to its customer in your Java code, but not the other way round.
In a bidirectional relationship, both entities reference each other. Order has customer, and Customer has a list of orders. That lets you navigate from either side, which is handy when you need it. But it comes with a cost. You have to use the mappedBy attribute on the non-owning side to tell JPA which side controls the foreign key, and you have to keep both sides synchronized in your code. If you add an order to the customer's list but forget to set the customer on the order, things get inconsistent.
There's also a big pitfall with bidirectional relationships: infinite loops during JSON serialization. If you return a Customer entity straight from a REST controller, Jackson serializes the orders list, each order references the customer again, and it loops forever and throws a stack overflow. You fix that with JSON annotations like JsonManagedReference and JsonBackReference, or JsonIgnore, or better yet by returning a DTO instead of the entity.
My rule is to start unidirectional by default, because it's simpler and safer, and only go bidirectional when I genuinely need to navigate from both sides — and then handle the synchronization and the JSON serialization carefully.
Unidirectional vs Bidirectional JPA Relationships
Direct Answer
Unidirectional means only one entity holds a reference to the other (Order knows Customer, but Customer doesn't list Orders). Bidirectional means both reference each other, requiring mappedBy on the inverse side. Bidirectional adds synchronization work and can cause infinite loops during JSON serialization if not handled.
Common Mistakes & Pitfalls
Making every relationship bidirectional by default, causing circular references in JSON and needless synchronization code
Check your understanding
Answer these without looking back at the article.
- What is the infinite loop problem with bidirectional JPA and JSON?
- What is @JsonManagedReference and @JsonBackReference?
- How do you maintain consistency in a bidirectional relationship?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.