Search code examples
kotlinktorkotlin-exposed

Parent with a child in the same type - how to map it?


I don't know how can I correctly convert child of ProductEntity (a type of ProductEntity) to 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?
)

At this time I have the error: Type checking has run into a recursive problem. Can you tell me how to fix it?


Solution

  • I found a solution:

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