mappedBy in JPA and Hibernate explained
Codemia Knowledge Hub ·
In JPA and Hibernate, mappedBy declares the inverse (non-owning) side of a bidirectional relationship. It tells the persistence provider "the foreign key lives on the other entity, in the field named by this string." Without mappedBy, Hibernate assumes both sides own the relationship and creates a redundant join table. The entity without mappedBy (the one with @JoinColumn) is the owning side, and only it controls inserts and updates to the foreign key column.
Owning side vs. inverse side
Every bidirectional relationship has exactly two sides:
Owning side: the entity whose table holds the foreign key column. It has @JoinColumn (or is the default owner in @ManyToMany). Changes to its collection or reference are written to the database.
Inverse side: the entity that declares mappedBy. The persistence provider treats it as read-only. Setting or adding entities on the inverse side alone produces no SQL INSERT or UPDATE for the foreign key.
This is the key to understanding mappedBy. Set both sides and Hibernate generates efficient SQL. Set only the inverse side and the relationship is silently not persisted.
One-to-many / many-to-one
This is the most common case. The "many" side always owns the relationship, because that is where the foreign key naturally lives.
mappedBy = "department" refers to the department field in Employee. From it, Hibernate knows that Employee.department_id is the foreign key column and that Department.employees is a mirror that produces no SQL of its own.
The SQL Hibernate generates
There is no join table. The foreign key sits on the employee table, where you want it.
What happens without mappedBy
Remove mappedBy from @OneToMany:
Hibernate now treats Department.employees as a unidirectional @OneToMany and creates a join table:
This is valid JPA, but it is almost never the intended design for a one-to-many relationship. It adds storage, complicates queries and hurts performance.
One-to-one
Either side can own a one-to-one relationship. The side with @JoinColumn is the owner.
The foreign key user_id lives in the user_profile table, and User.profile is the inverse side.
Lazy loading on the inverse @OneToOne side
On the inverse side (User.profile), Hibernate cannot tell whether the associated entity is null without querying the database. So FetchType.LAZY there is often not honored, and Hibernate issues an eager query to check whether a profile exists. The owning side (UserProfile.user) lazy-loads correctly, because Hibernate can read the user_id column.
If lazy loading on the inverse side matters for performance, share the primary key with @MapsId:
Many-to-many
A many-to-many relationship always needs a join table. One side must declare mappedBy, or Hibernate creates two join tables.
Student owns the relationship. Adding or removing entries in student.getCourses() triggers inserts and deletes in student_course. Changing only course.getStudents() does nothing in the database.
The mappedBy string must match the field name
The mappedBy value must exactly match the Java field name on the owning side. It is case-sensitive and refers to the Java property, not the database column.
If the string is wrong, Hibernate fails at startup:
This is easy to introduce when you rename a field during refactoring and forget the mappedBy string. IDEs like IntelliJ flag it, but not every refactoring tool does.
Keeping both sides in sync
Hibernate persists changes only from the owning side, so within the same persistence context (same transaction or session) you must set both sides, or the in-memory object graph becomes inconsistent.
The recommended pattern is a helper method on the parent that maintains both sides, like Department.addEmployee() above. Setting only the inverse side is the most frequent mistake: the entity appears in memory, but the foreign key is never written.
Quick reference
| Relationship | Owning side (has @JoinColumn) | Inverse side (has mappedBy) | Join table created |
@OneToMany / @ManyToOne | @ManyToOne side | @OneToMany side | No (FK on "many" table) |
@OneToOne | Side with @JoinColumn | Other side | No (FK on owner's table) |
@ManyToMany | Side with @JoinTable | Other side | Yes (one join table) |
@OneToMany without mappedBy | Both sides treated as owners | N/A | Yes (unintended join table) |
Other traps in bidirectional mappings
N+1 queries with eager fetching. FetchType.EAGER (the default for @ManyToOne and @OneToOne) loads associated entities immediately with each query, and for collections this produces N+1 queries. Use FetchType.LAZY and fetch explicitly with JOIN FETCH or @EntityGraph when you need the data.
Circular toString() and equals(). Bidirectional relationships create circular references. If both entities include each other in toString(), equals() or hashCode(), you get a StackOverflowError. Exclude the inverse side from these methods, or use only the entity's own fields (typically the primary key).
Related reading
- Can SQL reads return stale data?
- Can table columns with a Foreign Key be NULL?
- Can the DynamoDB single table design play nicely with a Microservices architecture?
- Can we have more than 1024 nodes in Couchbase?
- Can Spring Boot application handle multiple requests simultaneously?
- Can Spring Boot be used with OSGi? If not, any plans to have an OSGi Spring Boot?
- Can we make 2 phase commit protocol to be non-blocking if assumptions of 3PC used on it?
- Can we restore to same dynamodb table from backup
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.