【问题标题】:Parent with a child in the same type - how to map it?父母与同类型的孩子 - 如何映射它?
【发布时间】:2020-09-15 22:37:07
【问题描述】:

我不知道如何正确地将 ProductEntity(一种 ProductEntity)的子级转换为 Product。

class ProductEntity(id: EntityID<Int>) : BaseIntEntity(id, Products) {
    companion object : BaseIntEntityClass<ProductEntity>(Products)

    var name by Products.name
    var parentProduct by ProductEntity optionalReferencedOn Products.parentProduct

    fun toPojo() = Product(idValue, name, parentProduct?.toPojo())
}

data class Product(
    val id: Int,
    val name: String,
    val parentProduct: Product?
)

此时出现错误:类型检查遇到递归问题。 能告诉我怎么解决吗?

【问题讨论】:

  • 请在toPojo函数中添加显式类型:fun toPojo() : Product = Product(idValue, name, parentProduct?.toPojo())

标签: kotlin ktor kotlin-exposed


【解决方案1】:

我找到了解决办法:

class Product(productEntity: ProductEntity) {
    val id: Int = productEntity.idValue
    val name: String = productEntity.name
    val parentProduct = productEntity.parentProduct.toPojo()
}

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2012-12-29
    • 2018-01-15
    • 1970-01-01
    相关资源
    最近更新 更多