Kotlin 具有相同类型的子级的父级-如何映射它?

Kotlin 具有相同类型的子级的父级-如何映射它?,kotlin,ktor,kotlin-exposed,Kotlin,Ktor,Kotlin Exposed,我不知道如何正确地将ProductEntity(ProductEntity的一种类型)的子级转换为Product class ProductEntity(id: EntityID<Int>) : BaseIntEntity(id, Products) { companion object : BaseIntEntityClass<ProductEntity>(Products) var name by Products.name var pare

我不知道如何正确地将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?
)
class ProductEntity(id:EntityID):BaseIntEntity(id,Products){
伴生对象:BaseIntentyClass(产品)
按Products.name列出的变量名称
var parentProduct by ProductEntity optionalReferencedOn Products.parentProduct
fun toPojo()=产品(idValue、名称、父产品?.toPojo())
}
数据类产品(
val id:Int,
val name:String,
val parentProduct:产品?
)
此时我有一个错误:类型检查遇到了递归问题。 您能告诉我如何修复它吗?

我找到了一个解决方案:

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

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