정의
- one-to-one multiplicit를 가진 다른 entity들의 단일 연결을 정의한다.
- 일반적으로 참조되는 객체의 형식에서 관계를 유추할 수 있어 entity를 명시적으로 지정할 필요가 없다.
- 관계가 양방향인 경우 MappedBy 요소를 사용해 관계 필드 or 속성 필드를 지정해야 한다.
Example 1: One-to-one association that maps a foreign key column
@OneToOne(optional=false)
@JoinColumn(
name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
public CustomerRecord getCustomerRecord() { return customerRecord; }
@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.
@Entity
public class Employee {
@Id Integer id;
@OneToOne @MapsId
EmployeeInfo info;
...
}
@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 {
Class targetEntity() default void.class;
CascadeType[] cascade() default {};
FetchType fetch() default EAGER;
boolean optional() default true;
String mappedBy() default "";
boolean orphanRemoval() default false;
}