@Embedded와 @Embeddable는 관계형 데이터베이스에서 객체 간의 관계를 매핑하는 데 사용된다.
@Embedded@Embedded 클래스를 내장할 때 사용된다.@Embedded 어노테이션을 붙인다.@Entity
public class Student {
@Id
private Long id;
private String name;
@Embedded
private Address address;
}
위의 예제는
Address클래스를Student엔티티에 내장하겠다는 것을 의미한다.
→Student테이블에Address의 필드들이 칼럼으로 추가된다.
@Embeddable@Embeddable
public class Address {
private String street;
private String city;
}
Address클래스는 내장 가능한 클래스임을 나타내고 있다.
Address클래스의 인스턴스가 데이터베이스에 따로 테이블로 생성되지 않고,Student테이블의 칼럼으로 포함된다.
@Embeddable은 엔티티에 내장될 수 있는 클래스를 나타내고,@Embedded는 엔티티 클래스에서 실제로 내장 가능한 클래스를 사용하기 위해 지정하는 데 사용된다.