In JPA, @OneToOne maps a single-valued association between two entities, so that each instance of one entity refers to at most one instance of the other. Its significance is that it turns a foreign key (or shared primary key) in the database into a navigable object reference, while letting you control ownership, cascading, fetching and nullability through annotation attributes.
This article focuses on the attributes and the three standard ways to map the relationship in Hibernate 6 and Jakarta Persistence 3, with the SQL each produces.
@OneToOne supports these attributes (all optional):
| Attribute | Default | Effect |
|---|---|---|
optional | true | When false, the association must be non-null and Hibernate can use inner joins |
fetch | EAGER | LAZY lets the provider load the related entity on first access |
cascade | none | Propagates persist, merge, remove, refresh and detach |
mappedBy | none | Marks the inverse side and names the owning field |
orphanRemoval | false | Deletes the related entity when the reference is cleared |
targetEntity | inferred | Related entity class, rarely needed |
Everything about the physical column (name, nullability, uniqueness, foreign key name) comes from @JoinColumn on the owning side, not from @OneToOne itself.
The most common mapping stores the foreign key in the owner's table:
The resulting schema, written by hand in a migration, looks like this:
The unique constraint on parking_space_id is what guarantees that no two employees share a space. Persisting an Employee that references a new ParkingSpace also saves the space, because of CascadeType.PERSIST.
When the second entity is a mandatory extension of the first, use the same primary key in both tables:
@MapsId copies the customer's id into CustomerSettings.id, so no separate foreign key column or extra index is needed and entityManager.find(CustomerSettings.class, customerId) works directly.
To navigate from both sides, make one side the inverse:
Keep both references consistent in a helper (employee.assign(space) should set space.setEmployee(employee) too). Remember that only the owning side is written to the database, and that the inverse side loads eagerly in practice unless bytecode enhancement is configured, so use it sparingly.
@OneToOne when both sides are entities with their own identity and table.@ManyToOne with a unique constraint if you want a lightweight, always-lazy owning side without the inverse-side caveats.@Embedded (a value object stored in the same table) when the second object has no identity or lifecycle of its own, for example an Address stored as columns of customer.Fetch both entities in one statement with a join fetch to avoid a second query:
Turn on SQL logging (spring.jpa.show-sql=true or the org.hibernate.SQL logger) in tests and assert the number of statements, so an accidental eager load or extra select shows up early.
Not by itself. Add unique = true to the @JoinColumn (or a unique constraint in your migration script); with @MapsId the primary key is unique by definition.
It declares that the association must always be present, so Hibernate can validate non-null values, use inner joins and create lazy proxies more safely.
Yes, with @JoinTable instead of @JoinColumn, but a join table for a one-to-one is rarely justified. A foreign key column or shared primary key is simpler and faster.
The default fetch type is eager, and the inverse side is loaded with a secondary select. Set LAZY on the owning side, remove the inverse mapping if unused, or use a join fetch.
@OneToOne turns an exclusive database link into an object reference. Configure optional, fetch, cascade and mappedBy deliberately, put @JoinColumn with a unique constraint on the owning side, prefer @MapsId for mandatory extensions, and check generated SQL so the relationship stays cheap.