Quick revision
The short answer
The owning side is the one with the foreign key column, and it should be the child: UserProfile with a user_id column, or better, a profile that shares the user's primary key through @MapsId. The choice matters for lazy loading because the inverse, mappedBy side of a @OneToOne cannot be lazy without bytecode enhancement: Hibernate must know whether the value is null before it can decide to return null or a proxy. With @MapsId we can drop the inverse side and fetch the profile by the user's id when needed, so a unidirectional mapping from profile to user is often the simplest design.
What to remember
In this answer, we saw that the owning side of a one-to-one is the side with the foreign key, and it should be the child, UserProfile. The inverse mappedBy side cannot be lazy without bytecode enhancement, because Hibernate cannot tell null from a proxy without querying, which causes N+1 queries on user lists. Sharing the primary key with @MapsId lets us drop the inverse side and fetch a profile by the user's id, which makes a unidirectional mapping from profile to user the simplest design.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The owning side is the one with the foreign key column, and it should be the child: UserProfile with a user_id column, or better, a profile that shares the user's primary key through @MapsId. The choice matters for lazy loading because the inverse, mappedBy side of a @OneToOne cannot be lazy without bytecode enhancement: Hibernate must know whether the value is null before it can decide to return null or a proxy. With @MapsId we can drop the inverse side and fetch the profile by the user's id when needed, so a unidirectional mapping from profile to user is often the simplest design.
Why this question is asked
One-to-one relationships look like the simplest kind, yet they produce one of the most surprising performance problems in JPA: an association marked LAZY that loads eagerly anyway, adding a query for every user loaded. Interviewers ask about User and UserProfile to see whether we know why that happens and how the choice of owning side and keys avoids it. In this answer, we will recap the owning side of a one-to-one, see the two places the foreign key could live, reproduce the eager inverse side, fix it with @MapsId, and compare the options.
1A Quick Recap: Owning Side in a One-to-One
In a @OneToOne, one table holds a column that refers to the other table. The entity mapped to that table is the owning side, and the other side, if mapped at all, declares mappedBy. For User and UserProfile we have two choices: users.profile_id pointing to the profile, or user_profile.user_id pointing to the user.
The second is the natural one. A profile cannot exist without a user, a user may exist without a profile, and the profile is usually created after the user. With the foreign key on the child, creating a profile is one insert into user_profile; with it on the parent, creating a profile also requires updating the users row. So the child, UserProfile, should own the relationship.
2Why the Inverse Side Loads Eagerly
Here is the common bidirectional mapping, with LAZY requested on both sides.
| Side | Knows if value is null? | Lazy without enhancement? |
|---|---|---|
| Owning side (has the FK) | yes, from the FK column | yes, a proxy |
Inverse side (mappedBy) | no, needs a query | no, loaded eagerly |
| Inverse side + bytecode enhancement | loaded on first access | yes |
Profile with @MapsId, no inverse mapping | not needed | fetch by id when wanted |
The LAZY on User.profile is ignored. The reason is the meaning of a proxy. To give us a lazy reference, Hibernate must put either null or a proxy into the field. A proxy means there is a profile; null means there is none. On the owning side, the foreign key column answers that question for free: if user_profile.user_id is null, the field is null, otherwise it is a proxy for that id. On the inverse side, the users table has no column that says whether a profile exists, so Hibernate must query user_profile to find out, and once it has queried, it simply loads the profile. That is an N+1 pattern: 100 users cost 101 queries.
3Option 1: Bytecode Enhancement
Hibernate's bytecode enhancement rewrites entity classes at build time (through the Hibernate Maven or Gradle plugin) so that field access itself can trigger loading. With lazy loading enhancement enabled, the inverse side can stay unloaded until we call user.getProfile(), because the field no longer needs to hold null or a proxy up front. It works, but it adds a build step, changes how entities behave in debuggers and tests, and is easy to lose when someone changes the build. It is a reasonable tool, not a default.
4Option 2: Share the Primary Key With @MapsId
A cleaner design is to give the profile the same primary key as its user. @MapsId tells Hibernate that the profile's id is taken from its user association, so user_profile.user_id is both the primary key and the foreign key. Then the profile of user 42 is simply the profile with id 42, and we do not need a mapping on User at all.
Notice what changed. Loading users no longer touches user_profile, because User has no profile association to resolve. When a screen does need the profile, it asks for it by the user's id, a primary key lookup that is as cheap as it gets, and queries that need both can join them explicitly. The schema is also tighter: one index instead of a separate primary key plus a unique foreign key.
5Choosing the Mapping
For most cases the answer is a unidirectional mapping from profile to user, with @MapsId. It keeps the foreign key on the child, avoids the eager inverse side, and makes fetching explicit. If code genuinely needs user.getProfile() navigation everywhere, we can keep the bidirectional mapping and either accept the extra query (sometimes fine for single-entity screens), add bytecode enhancement, or fetch both together with a JOIN FETCH in the queries that list users. What we avoid is the bidirectional mapping with no plan, which quietly turns every user list into N+1 queries.
6Follow-up questions
Why not put the foreign key on User and make User the owner?
Then User.profile can be lazy, but creating a profile updates the users row, a user row must change whenever its profile is replaced, and a profile row no longer says which user it belongs to, so cleaning up profiles of deleted users becomes harder. It inverts the natural dependency, so it is rarely worth it.
Does optional = false on the inverse side make it lazy?
In older Hibernate versions it sometimes allowed a proxy, because a mandatory association is known never to be null. Behaviour has varied across versions, so check your version with a SQL log rather than relying on it.
How do we load users with their profiles for a list screen?
Either join fetch the profile from the owning side (select p from UserProfile p join fetch p.user) or use a DTO projection that joins both tables. Both are one query for the whole page.
7Conclusion
In this answer, we saw that the owning side of a one-to-one is the side with the foreign key, and it should be the child, UserProfile. The inverse mappedBy side cannot be lazy without bytecode enhancement, because Hibernate cannot tell null from a proxy without querying, which causes N+1 queries on user lists. Sharing the primary key with @MapsId lets us drop the inverse side and fetch a profile by the user's id, which makes a unidirectional mapping from profile to user the simplest design.
Open with
I'd make UserProfile the owning side, because the foreign key belongs on the child: a profile depends on a user, and the profile is usually created later, so inserting it shouldn't require updating the users table.
Then, if they want more
The interesting part is lazy loading. The owning side can be lazy without any tricks, because the foreign key column tells Hibernate whether there's a related row, so it can put in null or a proxy. The inverse mappedBy side, User.profile, can't.
The users table has nothing that says whether a profile exists, so Hibernate has to query user_profile to decide between null and a proxy, and at that point it just loads it. So FetchType.LAZY there is effectively ignored, and listing 100 users costs 101 queries.
You can fix that with bytecode enhancement, but I prefer a design fix: give the profile the same primary key as the user with @MapsId, so user_profile.user_id is both the primary key and the foreign key.
Then I don't need a profile field on User at all; when I need a profile I fetch it by the user's id, which is a primary key lookup, or join it explicitly in the queries that need both. So often the simplest mapping is unidirectional, from profile to user.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "For a bidirectional @OneToOne between User and UserProfile, which side should own the...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/a2430f211f/