영속성 전이 :CASCADE
·
JPA
특정 엔티티를 영속 상태로 만들 때 연관된 엔티티도 함께 연속성상태로 만들고 싶을 때 부모 엔티티르 저장할 때 자식 엔티티도 함께 저장 연관관계, 지연로딩 전혀 관계가 없음 @Entity public class Parent { @Id @GeneratedValue private Long id; private String name; @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List children = new ArrayList(); public void addChild(Child child){ children.add(child); child.setParent(this); } } @Entity public class Child { @..
영속성 전이 :CASCADE
·
JPA
특정 엔티티를 영속 상태로 만들 때 연관된 엔티티도 함께 연속성상태로 만들고 싶을 때 부모 엔티티르 저장할 때 자식 엔티티도 함께 저장 연관관계, 지연로딩 전혀 관계가 없음 @Entity public class Parent { @Id @GeneratedValue private Long id; private String name; @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List children = new ArrayList(); public void addChild(Child child){ children.add(child); child.setParent(this); } } @Entity public class Child { @..
즉시 로딩과 지연 로딩
·
JPA
Member를 조회할 때 Team도 함께 조회해야 할까? 지연 로딩 LAZYY을 사용해서 프록시 조회 @Entity public class Member extends BaseEntity{ @Id @GeneratedValue private Long id; @Column(name="USERNAME") private String username; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="TEAM_ID") private Team team; @OneToOne @JoinColumn(name="LOCKER_ID") private Locker locker; } FetchType.Lazy를 설정해준다면 해당 엔티티는 Proxy객체를 가져온다. 따라서 memberCla..
Proxy(프록시)
·
JPA
프록시 기초 JPA에는 데이터를 가져올 때 em.find() 뿐만 아니라 em.getReference()라는 메소드가 존재한다. - em.find(): 데이터베이스를 통해서 실제 엔티티 객체 조회 - em.getReference(): 데이터베이스 조회를 미루는 가짜(프록시)엔티티 객체 조회(데이터를 조회시 select 쿼리가 나가지 않음) public class JPAMain { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); EntityManager em = emf.createEntityManager(); EntityTransaction tx ..
쇼핑몰 만들기 3 - 상속관계 매핑
·
JPA
요구사항 상품의 종류는 음반, 도서, 영화가 있고 이후 더 확장될 수 있다. 모든 데이터는 등록일과 수정일이 필수다. 상속관계 만들기 @Entity @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn public abstract class Item { @Id @GeneratedValue @Column(name="ITEM_ID") private long id; private String name; private int price; private int stockQuantity; @ManyToMany(mappedBy = "items") private List categories = new ArrayList(); } Item 클래스만 단독..
@MappedSuperclass
·
JPA
공통 매핑 정보가 필요할 때 사용(id, name) - 테이블은 따로 사용하고 싶은데 객체 입장에서 따로 공통 정보만 상속 받아서 사용하고 싶을 때 상속관계 매핑X 엔티티X, 테이블과 매핑X 부모 클래스를 상속 받는 자식 클래스에 매핑 정보만 제공 조회, 검색 불가(em.find(BaseEntity) 불가) 직접 생성해서 사용할 일이 없으므로 추상 클래스 권장 @MappedSuperclass public abstract class BaseEntity { private String createdBy; private LocalDateTime createdDate; private String lastModifiedBy; private LocalDateTime lastModifiedDate; } @Entity ..
BE전문가 프로젝트
BE전문가 프로젝트