Skip to content

Latest commit

 

History

History
104 lines (81 loc) · 2.98 KB

OneToOne.md

File metadata and controls

104 lines (81 loc) · 2.98 KB

OneToOne Annotation

The @OneToOne annotation is used to define a one-to-one relationship between two entities in Java.

Attributes

  1. mappedBy (Optional): Specifies the name of the field in the inverse side of the relationship. This is used to map bidirectional associations. If not specified, it indicates that the relationship is unidirectional.

  2. cascade (Optional): Defines the cascade behavior for the relationship. It determines how operations on the owning entity affect the associated entity. By default, no cascading behavior is applied.

  3. fetch (Optional): Specifies the fetching strategy used to load the related entity. It determines when the associated entity should be loaded from the database. By default, eager fetching is used, meaning that the associated entity is loaded immediately along with the owning entity.

Usage

One main point! If you want to use relations you should use unique names of fields between entities!

import io.github.blyznytsiaorg.bibernate.annotation.enumeration.CascadeType;
import io.github.blyznytsiaorg.bibernate.annotation.enumeration.FetchType;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OneToOne {

    /**
     * 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.
     */
    String mappedBy() default "";

    /**
     * Defines the cascade behavior for the relationship.
     * By default, no cascading behavior is applied.
     */
    CascadeType[] cascade() default {};

    /**
     * Defines the fetching strategy used to load the related entity.
     * By default, eager fetching is used.
     */
    FetchType fetch() default FetchType.EAGER;
}

Examples

Unidirectional One-to-One Relationship

@Entity
@Table(name = "employees")
public class Employee {
    @Id
    @Column(name = "employees_id")
    private Long id;
    
    @OneToOne
    @JoinColumn(name = "employees_address_id")
    private Address address;

    // Other fields and methods
}

Bidirectional One-to-One Relationship

@Entity
@Table(name = "addresses")
public class Address {
    @Id
    @Column(name = "addresses_id")
    private Long id;

    @OneToOne(mappedBy = "address")
    private Employee employee;

    // Other fields and methods
}

One-to-One Relationship with Fetch Options

@Entity
@Table(name = "employees")
public class Employee {
    @Id
    @Column(name = "employees_id")
    private Long id;
    
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employees_address_id")
    private Address address;

    // Other fields and methods
}

See Also