Quick revision
The short answer
UserProfile gets @Id Long id and @OneToOne(fetch = LAZY) @MapsId @JoinColumn(name = "id") User user, so the profile's primary key is also its foreign key to the user. That saves a column and an index and guarantees at most one profile per user without a unique constraint. On the parent side, a mappedBy one-to-one usually still loads eagerly, because Hibernate must know whether a profile exists to choose between null and a proxy, and the only way to know is to query.
What to remember
In this answer, we mapped UserProfile with @MapsId so its primary key is also its foreign key to User, which saves a column and an index and makes one profile per user a guarantee. We saw that the mappedBy side cannot be a lazy proxy because Hibernate must query to know whether the profile exists, and that the options are to map only the owning side and fetch by id, to use bytecode enhancement, or to join fetch the profile when we need it.
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.
UserProfile gets @Id Long id and @OneToOne(fetch = LAZY) @MapsId @JoinColumn(name = "id") User user, so the profile's primary key is also its foreign key to the user. That saves a column and an index and guarantees at most one profile per user without a unique constraint. On the parent side, a mappedBy one-to-one usually still loads eagerly, because Hibernate must know whether a profile exists to choose between null and a proxy, and the only way to know is to query.
Why this question is asked
One-to-one relationships look like the simplest mapping in JPA, and they hide more surprises than any other. Teams split rarely used or sensitive columns into a profile table, mark the association lazy, and then find in the SQL log that every user load also loads its profile. Interviewers ask this question to see whether we know the cleanest way to map a one-to-one and the reason behind its best-known performance trap. In this answer, we will recap the two ways to link the tables, write the @MapsId mapping, see why the parent side cannot be lazy, and go through the options for dealing with it.
1Two Ways to Link a Profile to a User
The common first design gives user_profile its own id and a profile_id or user_id foreign key column. To guarantee one profile per user, we then need a unique constraint on that foreign key, which means a unique index. So the table has two indexes, one for its own primary key and one for the foreign key, and two columns that both identify the profile.
The shared primary key design removes one of each. The profile's primary key is the user's id. The column is both the primary key and the foreign key to the user table, so one index serves both purposes, and a primary key is unique by definition, so a user cannot have two profiles. Lookups are also simpler: to find a user's profile, we look it up by primary key with the user's id.
| Aspect | Own id plus user_id FK | Shared key with @MapsId |
|---|---|---|
| Columns that identify the profile | id and user_id | id only |
| Indexes | primary key plus unique index on user_id | primary key only |
| One profile per user | needs a unique constraint | guaranteed by the primary key |
| Find profile of user 42 | where user_id = 42 | where id = 42, a primary key lookup |
2Writing the @MapsId Mapping
@MapsId tells JPA that the value of the @Id field comes from the association. When we persist the profile, Hibernate copies the user's id into the profile's id; we never set it ourselves.
Notice the order. The profile's id comes from the user, so the user must be persisted first or in the same flush; persisting a profile whose user is new and not cascaded fails because there is no id to copy. Notice also that this mapping is unidirectional, from profile to user, and that the lazy @ManyToOne-style association on the profile side works perfectly: Hibernate knows the user id from the profile's own primary key, so it can create a proxy without any query.
3Why the Parent Side Still Loads Eagerly
Now suppose we also want user.getProfile(), so we add the other side to User.
- Does app_user hold a profile column?no,
mappedBysideYesUse it to decideNoMust look elsewhere - Query user_profile where id = 42the only way to know
- Row found?YesSet the loaded profileNoSet null
3 steps in order. Play them, or step through them one at a time.
The reason is in what a lazy association returns. For a lazy reference, Hibernate puts a proxy in the field and loads the data later. But a field can also be null, and for this field null means the user has no profile. The app_user row holds no column that says whether a profile exists; that fact lives only in user_profile. So to decide between null and a proxy, Hibernate has to look at user_profile, and once it has run that query, it may as well load the profile. The LAZY setting is a hint, and here Hibernate cannot follow it.
The owning side does not have this problem because its row holds the foreign key. A null foreign key means no user; a value means a user exists, and Hibernate creates a proxy with that id.
4The Options
The simplest option is to not map the parent side at all. The profile knows its user; the user does not hold a profile field. When we need the profile, we fetch it by id, which with a shared key is a primary key lookup: em.find(UserProfile.class, user.getId()) or profileRepository.findById(user.getId()). There is no hidden query on every user load.
The second option is Hibernate bytecode enhancement with lazy initialization enabled, through the Hibernate Gradle or Maven plugin. Enhanced classes intercept field access, so Hibernate can leave the field unloaded and query only when getProfile() is called. It works, but it changes the build and the behaviour of every entity, so it is a project-wide decision.
The third option is to keep the bidirectional mapping and fetch the profile deliberately in queries that need it, with left join fetch u.profile or an entity graph, so the extra rows come in the same query. That does not stop the per-row queries in other places, so it suits cases where most reads need the profile anyway.
5Follow-up questions
How would you model an optional one-to-one that is often absent?
I would map it only from the optional side, with a shared primary key, and never put a profile field on User. Code that needs the profile calls findById(userId) and gets an Optional, which makes the absence explicit. If the optional data is only a few small columns, I would also consider keeping them as nullable columns on the user table, because a separate table pays off mainly for large, rarely read or separately secured data.
Does optional = false on the mappedBy side make it lazy?
In Hibernate it does not reliably do so, because the parent row still has no column proving the profile exists; Hibernate would have to trust the annotation without checking. Behaviour has varied between versions, so check your version and the SQL log rather than relying on it.
Can I cascade from User to UserProfile with this mapping?
Only if the parent side is mapped, since cascades follow associations. With the unidirectional mapping, we persist the user and then the profile explicitly, or in a single service method in one transaction, which is usually clearer anyway.
6Conclusion
In this answer, we mapped UserProfile with @MapsId so its primary key is also its foreign key to User, which saves a column and an index and makes one profile per user a guarantee. We saw that the mappedBy side cannot be a lazy proxy because Hibernate must query to know whether the profile exists, and that the options are to map only the owning side and fetch by id, to use bytecode enhancement, or to join fetch the profile when we need it.
Open with
I would map UserProfile with @Id Long id without a generator, and @OneToOne(fetch = LAZY) @MapsId @JoinColumn(name = "id") User user. @MapsId copies the user's id into the profile's id at persist, so the primary key column is also the foreign key.
Then, if they want more
Compared with a separate user_id column, that saves a column and a unique index, guarantees at most one profile per user through the primary key itself, and makes finding a user's profile a primary key lookup. Because the id comes from the user, the user must be persisted first or in the same flush. The catch is the other side.
If User has @OneToOne(mappedBy = "user", fetch = LAZY) UserProfile profile, Hibernate still loads the profile with every user, because the user row has no column saying whether a profile exists. It has to choose between null and a proxy, and the only way to know is to query user_profile, so it loads it, giving N+1 for lists of users.
My default is to not map the parent side and to fetch the profile by id when needed, which with a shared key is cheap and explicit. If we really need the field, bytecode enhancement with lazy initialization makes it truly lazy, or we join fetch the profile in queries that need it.
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 "Map a User and UserProfile one-to-one so the profile table shares the user's primary...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/e177893858/