gpt4 book ai didi

spring - Kotlin DTO实体映射递归问题

转载 作者:行者123 更新时间:2023-12-02 13:41:21 24 4
gpt4 key购买 nike

所以我想将Dto映射到Entity,反之亦然,但是由于它们之间存在双向连接,因此我遇到了递归错误,对此有一个简单的解决方法,我刚刚开始使用kotlin并没有找到任何解决方案。
这些是我的扩展类。
RegistrationRuleExtension.kt

fun RegistrationRuleEntity.toDto() = RegistrationRuleDto(
id = id,
count = count,
description = description,
tier = tier,
licenses = licenses?.map { license -> license.toDto()}
)

fun RegistrationRuleDto.toEntity() = RegistrationRuleEntity(
id = id,
count = count,
description = description,
tier = tier,
licenses = licenses?.map { license -> license.toEntity()}
)

LicenseExtension.kt
fun LicenseEntity.toDto() = LicenseDto(
id = id,
name = name,
licenseId = licenseId,
rules = rules?.map { rule -> rule.toDto() },
version = version
)

fun LicenseDto.toEntity() = LicenseEntity(
id = id,
name = name,
licenseId = licenseId,
rules = rules?.map { rule -> rule.toEntity() },
version = version
)
如果我理解正确,那么问题是我的许可证在规则上为.toDto调用了,然后规则要在我的许可证上调用.toDto,它陷入了无限循环。
预先感谢您的答复!

最佳答案

因此,我从 friend 和同事那里得到了一些想法。
我的第一个想法:
由于许可证知道它具有哪些规则,因此我没有真正理由根据该规则获取许可证,因此我可以将其设置为空。
第二个想法:
我只将规则的ID存储在许可证上,如果我需要规则,则通过其ID来请求它们。
如果您还有其他想法,请随时对其进行评论。 :D

关于spring - Kotlin DTO实体映射递归问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63193752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com