[Spring Data JPA] @OnetoOne

DaeHoon·2022년 2월 14일
0

정의

  • one-to-one multiplicit를 가진 다른 entity들의 단일 연결을 정의한다.
  • 일반적으로 참조되는 객체의 형식에서 관계를 유추할 수 있어 entity를 명시적으로 지정할 필요가 없다.
  • 관계가 양방향인 경우 MappedBy 요소를 사용해 관계 필드 or 속성 필드를 지정해야 한다.
Example 1: One-to-one association that maps a foreign key column

    // On Customer class:

    @OneToOne(optional=false)
    @JoinColumn(
        name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
    public CustomerRecord getCustomerRecord() { return customerRecord; }

    // On CustomerRecord class:

    @OneToOne(optional=false, mappedBy="customerRecord")
    public Customer getCustomer() { return customer; }


    Example 2: One-to-one association that assumes both the source and target share the same primary key values. 

    // On Employee class:

    @Entity
    public class Employee {
        @Id Integer id;
    
        @OneToOne @MapsId
        EmployeeInfo info;
        ...
    }

    // On EmployeeInfo class:

    @Entity
    public class EmployeeInfo {
        @Id Integer id;
        ...
    }


    Example 3: One-to-one association from an embeddable class to another entity.

    @Entity
    public class Employee {
       @Id int id;
       @Embedded LocationDetails location;
       ...
    }

    @Embeddable
    public class LocationDetails {
       int officeNumber;
       @OneToOne ParkingSpot parkingSpot;
       ...
    }

    @Entity
    public class ParkingSpot {
       @Id int id;
       String garage;
       @OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;
        ... 
    } 

Option Element Summary

public @interface OneToOne {

    /** 
     * (Optional) The entity class that is the target of 
     * the association. 
     *
     * <p> Defaults to the type of the field or property 
     * that stores the association. 
     */
    Class targetEntity() default void.class;

    /**
     * (Optional) The operations that must be cascaded to 
     * the target of the association.
     *
     * <p> By default no operations are cascaded.
     */
    CascadeType[] cascade() default {};

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

    /** 
     * (Optional) Whether the association is optional. If set 
     * to false then a non-null relationship must always exist.
     */
    boolean optional() default true;

    /** (Optional) The field that owns the relationship. This 
      * element is only specified on the inverse (non-owning) 
      * side of the association.
     */
    String mappedBy() default "";


    /**
     * (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
     */
    boolean orphanRemoval() default false;
}
profile
평범한 백엔드 개발자

0개의 댓글