Annotation Interface OneToOne


@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface OneToOne
Specifies a single-valued association to another entity class that has one-to-one multiplicity. It is not usually necessary to specify the associated target entity explicitly since it can usually be inferred from the declared type of the annotated field or property.

A OneToOne association usually maps a unique foreign key relationship, either:

  • a foreign key column or columns with a unique constraint, or
  • a relationship via a shared primary key.

The JoinColumn annotation may be used to map the foreign key column or columns.

Here, a one-to-one association maps a foreign key column:

@Entity
public class Employee {
    @Id
    String employeeId;

    @OneToOne(optional = false)
    @JoinColumn(name = "INFO_ID", unique = true)
    EmployeeInfo info;
    ...
}

Here, the association is defined by a shared primary key:

@Entity
public class Employee {
    @Id
    String employeeId;

    @OneToOne
    @MapsId
    EmployeeInfo info;
    ...
}

@Entity
public class EmployeeInfo {
    @Id
    String employeeId;
    ...
}

Alternatively, an optional OneToOne association is sometimes mapped to a join table using the JoinTable annotation.

@Entity
public class Person {
    @Id
    String ssn;

    @OneToOne
    @JoinTable(name = "PERSON_SPOUSE",
               joinColumns = @JoinColumn(name = "PERSON_SSN"),
               inverseJoinColumns = @JoinColumn(name = "SPOUSE_SSN"))
    Person spouse;
    ...
}

The annotated field or property might represent one side of a bidirectional association. Every bidirectional association has an owning side and an inverse (alternatively, non-owning or unowned) side. Modifications to the owning side of an association determine the updates made to the relationship in the database. If the inverse side of an association is modified without a corresponding modification to the owning side, the behavior is undefined. The persistence provider is permitted to ignore any modification made only to the inverse side of a bidirectional association.

The inverse side of a bidirectional OneToOne association must be a field or property also annotated @OneToOne of the target entity, and the inverse side must specify the owning relationship field or property via mappedBy(). The foreign key or join table must be specified on the owning side.

@Entity
public class Customer {
    @Id @GeneratedValue
    Long id;

    // owning side
    @OneToOne(optional = false)
    @JoinColumn(name = "DETAILS_ID", unique = true)
    CustomerDetails customerDetails;
    ...
}

@Entity
public class CustomerDetails {
    @Id @GeneratedValue
    Long id;

    // inverse (unowned) side
    @OneToOne(optional = false,
              mappedBy = Customer_.CUSTOMER_DETAILS)
    Customer customer;
    ...
}

The OneToOne annotation may be used within an embeddable class to specify a relationship from the embeddable class to an entity class. If the relationship is bidirectional and the entity containing the embeddable class is on the owning side of the relationship, the non-owning side must use the mappedBy() element of the OneToOne annotation to specify the relationship field or property of the embeddable class. The dot (.) notation syntax must be used in the mappedBy() element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

@Entity
public class Employee {
    @Id
    int id;

    @Embedded
    LocationDetails location;
    ...
}

@Embeddable
public class LocationDetails {
    int officeNumber;

    // owning side
    @OneToOne
    ParkingSpot parkingSpot;
    ...
}

@Entity
public class ParkingSpot {
    @Id
    int id;

    String garage;

    // inverse (unowned) side
    @OneToOne(mappedBy = "location.parkingSpot")
    Employee assignedTo;
    ...
}
Since:
1.0
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    (Optional) The operations that must be cascaded to the target of the association.
    (Optional) Whether the association should be lazily loaded or must be eagerly fetched.
    The field or property of the target entity that owns the relationship and specifies its mapping to the database.
    boolean
    (Optional) Whether the association is optional.
    boolean
    (Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.
    (Optional) The entity class that is the target of the association.
  • Element Details

    • targetEntity

      Class<?> targetEntity
      (Optional) The entity class that is the target of the association. Required if the target entity type cannot be inferred from the declared type of the annotated field or property.

      Defaults to the declared type of the annotated field or property.

      Default:
      void.class
    • cascade

      CascadeType[] cascade
      (Optional) The operations that must be cascaded to the target of the association.

      By default, no operations are cascaded.

      Default:
      {}
    • fetch

      FetchType fetch
      (Optional) Whether the association should be lazily loaded or must be eagerly fetched.
      • The EAGER policy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched.
      • The LAZY policy is a hint to the persistence provider runtime.

      When fetch is not explicitly specified, the fetching strategy is determined by the default fetch type of the persistence unit for one-to-one and many-to-one associations.

      Default:
      DEFAULT
    • optional

      boolean optional
      (Optional) Whether the association is optional.
      • By default, the association is optional, and the annotated field or property may be set to a null value.
      • When optional=false, a non-null value must be assigned to the non-optional field or property before the persistence provider attempts to write the state of entity instance to the database.

      If the annotated field or property is also annotated @jakarta.annotation.Nonnull, then optional=false is implied, and the value of this annotation member is ignored.

      May be used in schema generation to infer that the mapped foreign key column is not null.

      Default:
      true
    • mappedBy

      String mappedBy
      The field or property of the target entity that owns the relationship and specifies its mapping to the database. This element is only specified on the inverse (non-owning) side of the association.

      The static metamodel of the target entity may be used to obtain a reference to the owning side, for example, mappedBy = Customer_.CUSTOMER_DETAILS.

      Default:
      ""
    • orphanRemoval

      boolean orphanRemoval
      (Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.
      Since:
      2.0
      Default:
      false