김영한 선생님 3

[JPA] 값 타입 Collections

데이터베이스에는 자바의 컬렉션을 저장할 수 있는 메커니즘이 아니다. 자바의 컬렉션 데이터를 저장하려면 별도의 테이블에 하나하나 저장해야한다. 사용 방법 예시 코드 Address 임베디드 값 타입 : @Embeddable public class Address { private String city; private String street; private String zipcode; public Address() { } public Address(String city, String street, String zipcode) { this.city = city; this.street = street; this.zipcode = zipcode; } public String getCity() { return cit..

JPA 2020.10.24

[JPA] CASCADE

특정 엔티티를 영속 상태로 만들 때, 연관된 엔티티도 함께 영속 상태로 만들도 싶을 때 사용한다. 예를 들어, 부모 엔티티를 저장할 때 자식 엔티티도 함께 저장할때 사용한다. 원래 기본은 Child child1 = new Child(); Child child2 = new Child(); Parent parent = new Parent(); parent.addChild(child1); parent.addChild(child2); em.persist(parent); em.persist(child1); em.persist(child2); 이렇게 child1, 2를 다 저장해야한다. 근데, @Entity public class Parent { @Id @GeneratedValue private Long id; p..

JPA 2020.10.22

[JPA] 지연로딩

참조가 있는 엔티티를 조회할때는 그 참조를 한번에 같이 즉시 조회할지 아니면 사용할때 조회할지에 대한 정의를 내려줘야한다. 즉시 로딩은 엔티티 객체 조회시에 한번에 다 가져오는 것이고, 지연 로딩은 참조의 메서드를 호출 했을 때, 초기화했을 때 그때 불러오는 것이다. @ManyToOne과 @OneToMany의 fetch 전략은 기본이 즉시로딩이다. 이것을 지연로딩으로 바꾸려면 아래 코드와 같다. @Entity public class Member extends BaseEntity { @Id @GeneratedValue @Column(name = "MEMBER_ID") private Long id; @Column(name = "USERNAME") private String username; @ManyToOn..

JPA 2020.10.22