(Persistence)영속성 - DB

이상원·2023년 3월 28일
0

영속성(Persistence)

영속성(persistence)은 데이터를 생성한 프로그램의 실행이 종료되더라도 사라지지 않는 데이터의 특성을 의미한다. 영속성은 파일 시스템, 관계형 테이터베이스 혹은 객체 데이터베이스 등을 활용하여 구현한다. 영속성을 갖지 않는 데이터는 단지 메모리에서만 존재하기 때문에 프로그램을 종료하면 모두 잃어버리게 된다. 결국 영속성은 특정 데이터 구조를 이전 상태로 복원할 수 있게 해주어 프로그램의 종료와 재개를 자유롭게 해준다.
the characteristic of data that outlives the execution of the program that created it: which is achieved in practice by storing the data in non-volatile storage such as a file system or a relational database or an object database

영속성 전이(Cascade)

엔티티의 상태를 변경할 때 해당 엔티티와 연관된 엔티티의 상태 변화를 전파시키는 옵션

  • 무분별하게 사용시 삭제되지 말아야 할 데이터가 삭제될 수 있음

CASCADE 옵션

The cascade types supported by the Java Persistence Architecture are as below:

  1. CascadeType.PERSIST: cascade type presist means that save() or persist() operations cascade to related entities.
  2. CascadeType.MERGE: cascade type merge means that related entities are merged when the owning entity is merged.
  3. CascadeType.REFRESH: cascade type refresh does the same thing for the refresh() operation.
  4. CascadeType.REMOVE: cascade type remove removes all related entities association with this setting when the owning entity is deleted.
  5. CascadeType.DETACH: cascade type detach detaches all related entities if a “manual detach” occurs.
  6. CascadeType.ALL: cascade type all is shorthand for all of the above cascade operations.

Spring data jpa에서의 궁금증


지연 로딩(FetchType.LAZY)

일대일, 다대일로 매핑할 경우 기본 전략 즉시 로딩을 통해 엔티티
하지만 복잡한 비즈니스 로직일 경우 한번에 join 될 시 매핑되는 엔티티의 개수가 훨씬 많게 됨
성능 문제가 발생할 수 있음 따라서 지연 로딩 방식을 이용해야 함

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id")
    private Member member;
profile
개발하는 코린이

0개의 댓글