Development/Java
@ElementCollection
폴피드
2022. 6. 13. 15:10
- 컬랙션 객체임을 JPA 에 알려주는 Annotation.
@Entity public class Person { @Id private Long id; private String email; @ElementCollection @CollectionTable( name = "address", joinColumns = @JoinColumn(name = "person_id") ) List<AddressInfo> addressInfoList = new ArrayList<>(); }
- Entity 와 라이프 싸이클을 같이 하며 독립적으로 사용 불가능 하다.
- 부모 Entity가 삭제될 경우 같이 삭제된다. (실제 클래스에 cascade 를 설정하는 옵션이 없다.)
- ElementCollection의 Fetch 전략은 기본이 Lazy 이다.
- 실제 테이블은 FK 를 이용해서 생성된다.
Hibernate: create table address (person_id bigint not null, address1 varchar(255), address2 varchar(255), zip_code varchar(255)) Hibernate: create table person (id bigint not null, email varchar(255), primary key (id)) Hibernate: alter table address add constraint FK81ihijcn1kdfwffke0c0sjqeb foreign key (person_id) references person
- CollectionTable Annocation 을 사용하지 않을 경우에는 다음과 같이 테이블이 생성된다.
Hibernate: create table person_address_info_list (person_id bigint not null, address1 varchar(255), address2 varchar(255), zip_code varchar(255))
728x90
반응형