Annotation Interface OneToOne


@Target(FIELD) @Retention(RUNTIME) public @interface OneToOne
Specifies a one-to-one relationship between two entities. This annotation is used to configure the mapping between the owning side and the inverse side of the relationship.

Example usage:


   @Entity
   public class EntityA {
       @OneToOne(mappedBy = "entityA", cascade = CascadeType.DELETE, fetch = FetchType.LAZY)
       private EntityB entityB;
       // Other fields and methods
   }

   @Entity
   public class EntityB {
       // No need to specify mappedBy when the relationship is unidirectional
       @OneToOne(cascade = CascadeType.DELETE, fetch = FetchType.LAZY)
       private EntityA entityA;
       // Other fields and methods
   }
 
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Defines the cascade behavior for the relationship.
    Defines the fetching strategy used to load the related entity.
    The name of the field in the inverse side of the relationship.
  • Element Details

    • mappedBy

      String mappedBy
      The name of the field in the inverse side of the relationship. This is required unless the relationship is unidirectional. Defaults to an empty string, indicating that the relationship is not mapped by another field.
      Default:
      ""
    • cascade

      CascadeType[] cascade
      Defines the cascade behavior for the relationship. By default, no cascading behavior is applied.
      Default:
      {}
    • fetch

      FetchType fetch
      Defines the fetching strategy used to load the related entity. By default, eager fetching is used.
      Default:
      EAGER