songining
article thumbnail

ํŠน์ • ํ…Œ์ด๋ธ”์•ˆ์— ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ 

RDBMS๋Š” ํ…Œ์ด๋ธ” ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ์ด ๋ถˆ๊ฐ€. ์ฆ‰ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”๋กœ ๋นผ๋‚ด์•ผ ํ•จ

(๋นผ๋‚ธ ํ…Œ์ด๋ธ”์€ Id์™€ ์ปฌ๋Ÿผ ์ „๋ถ€๋ฅผ pk๋กœ ๊ฐ€์ง„๋‹ค.)

 

Ex) 

//์ฃผ์†Œ
@Embedded
private Address homeAddress;

//HashSet 
@ElementCollection
@CollectionTable(name = "favorite_food", joinColumns =
    @JoinColumn(name = "member_id")) // FK๋กœ ์žก๊ธฐ ์œ„ํ•จ
private Set<String> favoriteFoods = new HashSet<>();

//ArrayList
@ElementCollection
@CollectionTable(name = "address", joinColumns =
    @JoinColumn(name = "member_id"))
@Column(name = "food_name")
private List<Address> addressHistory = new ArrayList<>();
Member member = new Member();
member.setUsername("member1");
member.setHomeAddress(new Address("homeCity","street","1111"));

member.getFavoriteFoods().add("์น˜ํ‚จ");
member.getFavoriteFoods().add("์กฑ๋ฐœ");
member.getFavoriteFoods().add("ํ”ผ์ž");

member.getAddressHistory().add(new Address("old1","street","1111"));
member.getAddressHistory().add(new Address("old2","street","1111"));

em.persist(member);

์‹คํ–‰๊ฒฐ๊ณผ member์™€ address, favorite food์— ์ „๋ถ€ ๊ฐ’์ด ๋งž๊ฒŒ ๋“ค์–ด๊ฐ„๋‹ค. 

(em.persist ํ•œ๋ฒˆ ์‹คํ–‰์„ ํ†ตํ•ด ์ „๋ถ€ ๊ฐ’์ด ๋“ค์–ด๊ฐ) ->

why? ๊ฐ’ ํƒ€์ž… ์ปฌ๋ ‰์…˜๋„ ๊ฐ’ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— member์— ์˜์กด. 

์˜์†์„ฑ ์ „์ด(cascade) + ๊ณ ์•„๊ฐ์ฒด ์ œ๊ฑฐ ๊ธฐ๋Šฅ์„ ํ•„์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค.

๊ฐ’ ํƒ€์ž… collection์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์—ฐ๋กœ๋”ฉ 

 

๊ฐ’ ํƒ€์ž… ์ปฌ๋ ‰์…˜ update๋Š” ์–ด๋–ป๊ฒŒ? 

Ex1) 

//์น˜ํ‚จ -> ํ•œ์‹
findMember.getFavoriteFoods().remove("์น˜ํ‚จ");
findMember.getFavoriteFoods().add("ํ•œ์‹");

์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•จ 

 

Ex2) 

//old1 -> newCity1
findMember.getAddressHistory().remove(new Address("old1", "street", "1111"));
findMember.getAddressHistory().add(new Address("newCity1","street","1111"))

+) ๊ทธ๋ƒฅ ๊ฐ’ ํƒ€์ž…์˜ ๋ณ€๊ฒฝ์€? 

Address a = findMember.getHomeAddress();
findMember.setHomeAddress(new Address("newCity", a.getStreet(), a.getZipcode()));

์œ„์™€ ๊ฐ™์ด ์ƒˆ๋กœ ๊ฐ์ฒด ๊ฐˆ์•„๋ผ์›Œ์•ผ ํ•จ 

 

๊ฒฐ๋ก  :

- ๊ฐ’ ํƒ€์ž… ์ปฌ๋ ‰์…˜์˜ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์ปฌ๋ ‰์…˜์ด ๋“ค์–ด์žˆ๋Š” Member(์˜ˆ์‹œ)์— ์˜์กด๊ด€๊ณ„๋ฅผ ์ „๋ถ€ ๋งก๊ธด๋‹ค. 

- ๊ฐ’ ์ถ”์ ์ด ๊ต‰์žฅํžˆ ์–ด๋ ต๋‹ค. -> ์‹ค๋ฌด์—์„œ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ’ ํƒ€์ž… ์ปฌ๋ ‰์…˜ ๋Œ€์‹ ์— ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ๊ณ ๋ ค

 

์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋กœ ํ’€์ž!!!(๋‹จ๋ฐฉํ–ฅ)


    //    @ElementCollection
//    @CollectionTable(name = "address", joinColumns =
//        @JoinColumn(name = "member_id"))
//    @Column(name = "food_name")
//    private List<Address> addressHistory = new ArrayList<>();

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "member_id")
    private List<AddressEntity> addressHistory = new ArrayList<>();
    
    //AddressEntity๋Š” ํ‰์ƒ์‹œ ์—”ํ‹ฐํ‹ฐ ๋งŒ๋“ค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.(๊ธฐ๋ณธ ์—”ํ‹ฐํ‹ฐ)

๊ฐ’ํƒ€์ž… ์ปฌ๋ ‰์…˜๋ณด๋‹ค ์ด๋ ‡๊ฒŒ ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋กœ ํ‘ธ๋Š”๊ฒƒ์ด ๊ฐ’ ๋ณ€๊ฒฝ์ด๋‚˜ ์—ฌ๋Ÿฌ ์ธก๋ฉด์—์„œ ํ›จ์”ฌ ์ข‹์Œ.

 

๊ฒฐ๊ณผ :

 

-> ๊ฐ’ํƒ€์ž…์€ ์ •๋ง ๊ฐ’ ํƒ€์ž…์ด๋ผ ํŒ๋‹จ๋  ๋•Œ๋งŒ ์‚ฌ์šฉ (์ง€์†์ ์œผ๋กœ ๊ฐ’ ์ถ”๊ฐ€ ๋ฐ ๋ณ€๊ฒฝ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ฐ’ ํƒ€์ž…์ด ์•„๋‹ˆ๋ผ ์—”ํ‹ฐํ‹ฐ!!)

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

SpringBoot API ์ฃผ์˜ํ•  ์   (2) 2022.01.27
๊ฐ์ฒด์ง€ํ–ฅ์ฟผ๋ฆฌ  (0) 2022.01.19
์ž„๋ฒ ๋””๋“œ ํƒ€์ž… ์‚ฌ์šฉ๋ฐฉ๋ฒ•  (0) 2022.01.18
SpringBoot JPA CASCADE ์˜ˆ์ œ  (0) 2022.01.17
ํ”„๋ก์‹œ  (0) 2022.01.16