spring - Kotlin DTO 实体映射递归问题
问题描述
所以我试图将 Dto 映射到实体,反之亦然,但由于我在它们之间有双向连接,我遇到了递归错误,是否有一个简单的解决方法,我刚开始使用 kotlin,还没有找到任何解决方案。
这些是我的扩展类。
注册规则扩展.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()}
)
许可证扩展.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 并且它进入无限循环。
感谢您提前回答!
解决方案
所以我从朋友和同事那里得到了一些想法。
我的第一个想法:
由于许可证知道它有哪些规则,因此我没有理由根据规则获得许可证,因此我可以将其设置为空。
第二个想法:
我只将规则的 ID 存储在许可证上,如果我需要规则,我会通过他们的 ID 请求它们。
如果您有任何其他想法,请随时发表评论。:D
推荐阅读
- php - 如何使用 Laravel 获取以前的外部链接?
- sql-server - 使用 Entity Framework Core 在 SQL Server 中存储大字符串的最佳实践
- iis-7 - IIS 管理器中的停止服务器是否与 iisReset /stop 相同?
- java - 在 AWS Fargate 上运行时如何确定当前可用区
- java - 坚持使用这个单词搜索程序
- python - Django 测试客户端:轮询应用程序第 5 部分 NoReverseMatch
- python - 使用 tox 的虚拟环境 PYTHONPATH
- typescript - 如何设置 nswag swagger2tsclient 获取选项,如凭据:“包括”?
- c# - 带有流利的api的实体框架唯一键?
- oauth - HybridAuth - 身份验证后继续脚本并获得结果