์• ์ •์ฝ”๋”ฉ ๐Ÿ’ป

Project/DARLING 2022.01.23 ๋Œ“๊ธ€ 0๊ฐœ Joana

4. ์—ฐ๊ด€๊ด€๊ณ„ - OneToMany

DiaryEntity

package so.ego.re_darling.domains.diary;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import so.ego.re_darling.domains.user.domain.Couple;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "diary")
@Entity
public class Diary {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private LocalDateTime date;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "couple_id")
    private Couple couple;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "diary_id")
    private List<DiaryComment> diaryCommentList = new ArrayList<>();


}

 

Diary ๊ธ€ ํ•œ๊ฐœ์˜ ์—ฌ๋Ÿฌ๊ฐœ์˜ Comment๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1. Diary๋ฅผ ์กฐํšŒํ•  ๋•Œ comment List ๋„ ๊ฐ™์ด ์กฐํšŒํ•˜๊ณ  ์‹ถ๋‹ค.

2. Comment๋ฅผ ๋”ฐ๋กœ ์กฐํšŒํ•  ๋ชฉ์ ์ด ์—†๋‹ค.

 

๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ์ผ๋Œ€๋‹ค ์ค‘ ์ผ์ธ Diary์— @OneToMany๋ฅผ ์ ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค . ํ•˜์ง€๋งŒ ํ๋ฆ„์„ ์ƒ๊ฐํ•ด๋ณด๋ฉด

Diary ์ž‘์„ฑ -> Comment ์ž‘์„ฑ -> Diary update query ๋ฐœ์ƒ ์ด ๋œ๋‹ค.

๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”์˜ ํŒจ๋Ÿฌ๋‹ค์ž„ ์ฐจ์ด๋•Œ๋ฌธ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ ๋‹ค๋Œ€์ผ ๋‹จ๋ฐฉํ–ฅ ๊ด€๊ณ„๋กœ ๋งคํ•‘ ํ›„ ํ•„์š”ํ•  ๋•Œ ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์„ ํ•˜๋„๋ก ํ•œ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

'Project > DARLING' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

7. CI/CD ๊ณ„ํš  (0) 2022.02.09
6. ๋ฌด์—‡์ด ๋ฌธ์ œ์ผ๊นŒ ...  (0) 2022.01.25
5. Spring REST Docs ์‚ฌ์šฉ  (0) 2022.01.24
4. ์—ฐ๊ด€๊ด€๊ณ„ - OneToMany  (0) 2022.01.23
3. ์—ฐ๊ด€๊ด€๊ณ„ - ManyToOne  (0) 2022.01.23
2. Entity  (0) 2021.12.03