연관관계와 mutableList, add와의 관계

0

Kotlin

목록 보기
13/32

mutable + add! 가 꼭 필요한 것 같다.
양방향 연관관계에서 양쪽 모두에 관계를 맺어주는 건 이해가 가는데, copy는 안되고 꼭 add를 통해서 OneToMany에 주인을 넣어줘야하는건지 모르겠다....아무리 찾아도 없네 ㅠㅠ

val drugList = request.drugs.map { request ->
//            Drug.of(request)
//                .copy(userId = user.id)
//                .let { drug ->
//                    val copy = drug.copy(
//                        drugDetail = request.times.map {
//                            DrugDetail(drug = drug, time = it)
//                        }
//                    )
//                    copy.drugDetail.forEach {
//                        println("it: ${it.drug.id}")
//                    }
//                    println("copy.drugDetail: ${copy.drugDetail}")
//                    println("copy: ${copy}")
//                    copy
//                }
//        }


        /*
        val drugList0 = request.drugs.map { request ->
            Drug.of(request).copy(
                userId = user.id,
            ).apply {
                request.times.map {
                    this.drugDetail.add(DrugDetail(drug = this, time = it))
                }
            }
        }
        */
        /*
        val drugList0 = request.drugs.map { request ->
            Drug.of(request)
                .copy(userId = user.id)
                .apply {
                    drugDetail.addAll(request.times.map { DrugDetail(drug = this, time = it) })
                }
        }

        val drugDetail = DrugDetail(id = 1)
        drugDetail.drug.name

        for (item in drugList0) {
            drugRepository.save(item)
        }
        DrugResponseImpl(drugList0)
        */



//
//
//        for (item in drugList) {
//            logger.info("item1 : ${item.id}")
//            val it = drugRepository.save(item)
//            logger.info("item2 : ${it.id}")
//            logger.info("item detail1 : ${it.drugDetail[0].id}")
//            logger.info("item detail2 : ${it.drugDetail[0].drug.id}")
//        }
//        DrugResponseImpl(drugList)


        val drugList5 = request.drugs.map { request ->
            Drug.of(request)
                .copy(userId = user.id)
                .apply {
                    val details = request.times.map { DrugDetail(drug = this, time = it) }
                    copy(
                        drugDetail = drugDetail.apply { addAll(details) },
                    )
                    /*
                    copy(
                        drugDetail = details.toMutableList(),
                    )
                    */
                }
        }
        for (item in drugList5) {
            drugRepository.save(item)
        }
        DrugResponseImpl(drugList5)

        /*
        val drugList1 = request.drugs.map { request ->
            Drug.of(request)
                .copy(userId = user.id)
                .run {
                    copy(drugDetail = request.times.map { DrugDetail(drug = this, time = it) }.toMutableList())
                }
        }

        for (item in drugList1) {
            drugRepository.save(item)
        }
        DrugResponseImpl(drugList1)
        */

        /*
        val drugList2 = request.drugs.map { request ->
            Drug.of(request)
                .copy(userId = user.id)
                .apply {
                    request.times
                        .map { DrugDetail(drug = this, time = it) }
                        .forEach { drugDetail.toMutableList().add(it) }
                }
        }

        for (item in drugList2) {
            drugRepository.save(item)
        }
        DrugResponseImpl(drugList2)
        */

        /*
        val drugList3 = request.drugs.map { request ->
            Drug.of(request)
                .copy(userId = user.id)
                .apply {
                    drugDetail.toMutableList().addAll(request.times.map { DrugDetail(drug = this, time = it) })
                }
        }

        for (item in drugList3) {
            drugRepository.save(item)
        }
        DrugResponseImpl(drugList3)
        */
profile
백엔드를 공부하고 있습니다.

0개의 댓글