STEP 1 (다대일 단방향)
책 : 서점 = N : 1 이라고 가정하고 제출한 답안
package com.example.spring_week_2_test.entity;
import com.example.spring_week_2_test.dto.BookRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "Book") //테이블명 선언
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id") //PK설정
private Long id;
@Column(nullable = false)
private String author;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int price;
@Column(nullable = false)
private Long quantity;
@ManyToOne
@JoinColumn(name = "book_store_id", nullable = false)
private BookStore bookStore; //FK는 book_store_id 가 된다.
}
package com.example.spring_week_2_test.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "BookStore")
public class BookStore {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_store_id")
private Long id;
@Column(nullable = false)
private String location;
@Column(nullable = false)
private String name;
}
STEP2 ( 다대일 양방향 )
책 : 서점 = N : 1 이라고 가정하고 제출한 답안
package com.example.spring_week_2_test.entity;
import com.example.spring_week_2_test.dto.BookRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "Book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private Long id;
@Column(nullable = false)
private String author;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int price;
@Column(nullable = false)
private Long quantity;
@ManyToOne
@JoinColumn(name = "book_store_id", nullable = false)
private BookStore bookStore;
}
package com.example.spring_week_2_test.entity;
import com.example.spring_week_2_test.dto.BookStoreRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "BookStore")
public class BookStore {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_store_id")
private Long id;
@Column(nullable = false)
private String location;
@Column(nullable = false)
private String name;
@OneToMany(mappedBy = "bookStore")
private List<Book> bookList = new ArrayList<>();
}
STEP3. 일대다 단방향 연관관계
책 : 회원 = N : 1 이라고 가정하고 제출한 답안
package com.example.spring_week_2_test.entity;
import com.example.spring_week_2_test.dto.BookStoreRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "BookStore")
public class BookStore {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_store_id")
private Long id;
@Column(nullable = false)
private String location;
@Column(nullable = false)
private String name;
@OneToMany(mappedBy = "bookStore")
private List<Book> bookList = new ArrayList<>();
@OneToMany
@JoinColumn(name = "book_store_id")
private List<Member> memberList = new ArrayList<>();
}
package com.example.spring_week_2_test.entity;
import jakarta.persistence.*;
@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
private Long id;
@Column(nullable = false)
private String address;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String nickname;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String phoneNumber;
}
STEP4. 구매(Purchase) Entity를 사용해서 (Member)과 책(Book)의 연관관계를 맺어보세요!
구매 : 회원 = N : 1
구매 : 책 = N : 1
이라고 가정하여 작성한 답안.
구매와 책 관계가 헷갈렸는데, 코드를 생각해보면 해당 책에 대한 코드는 하나일 것이고, 그 책에 대한 구매가 여러번 발생할 것으로 생각하면 이해가 조금 쉽다 !
package com.example.spring_week_2_test.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "Purchase")
public class Purchase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "purchase_id")
private Long id;
@ManyToOne
@JoinColumn(name = "member_id")
private Member member;
@ManyToOne
@JoinColumn(name = "book_id")
private Book book;
}
package com.example.spring_week_2_test.entity;
import com.example.spring_week_2_test.dto.BookRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "Book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id")
private Long id;
@Column(nullable = false)
private String author;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int price;
@Column(nullable = false)
private long quantity;
@ManyToOne
@JoinColumn(name = "book_store_id", nullable = false)
private BookStore bookStore;
@OneToMany(mappedBy = "book")
private List<Purchase> purchases = new ArrayList<>();
}
package com.example.spring_week_2_test.entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
private Long id;
@Column(nullable = false)
private String address;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String nickname;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String phoneNumber;
@OneToMany(mappedBy = "member")
private List<Purchase> purchaseList = new ArrayList<>();
}
스텝 4 까지 했을 때 ERD
틀린 부분
1. Book Entity의 quantity 타입을 long 으로 했어야 하는데 Long 으로 작성했다.
2. STEP4 Purchase 부분 지연로딩 (fetch = FetchType.LAZY) 추가
package com.example.spring_week_2_test.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "Purchase")
public class Purchase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "purchase_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY) //(fetch = FetchType.LAZY) 추가
@JoinColumn(name = "member_id")
private Member member;
@ManyToOne(fetch = FetchType.LAZY) //(fetch = FetchType.LAZY) 추가
@JoinColumn(name = "book_id")
private Book book;
}
참고 자료 :
'Coding > Spring' 카테고리의 다른 글
[24] ERD(Entity Relationship Diagram) (0) | 2022.12.13 |
---|---|
[23] Spring Security (0) | 2022.12.13 |
[21] ORM, JPA, Spring Data JPA (0) | 2022.12.08 |
[20] 의존성 주입 DI(Dependency Injection) (0) | 2022.12.08 |
[19] 스프링 프레임워크 (0) | 2022.12.08 |