You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

继承带参数抽象类GraphUnit时编译失败的问题咨询

问题:JPA实体继承抽象类时的无参构造器错误

原代码实现

定义了如下抽象类:

abstract class GraphUnit(var newsSources:Set<NewsSource>): NewsParent {
    //...
    fun getImportantNewsCompleted(): List<Informacion> {
        //Do something with the news sources...
    }
}

通过以下方式实现JPA实体:

@Entity
class Scenario (
    @OneToMany(mappedBy = "scenario")
    val tags: Set<Tag>
): GraphUnit(tags), NewsSource {
    //...
}

@Entity
class Tag (
   //...
):NewsSource{
    //...
}

编译错误信息

编译时抛出如下异常:

java.lang.IllegalStateException: No noarg super constructor for CLASS
CLASS name:Scenario modality:FINAL visibility:public
superTypes:[com...kotlinbase.database.entity.interfaces.GraphUnit;
com...kotlinbase.database.entity.interfaces.NewsSource]:
CONSTRUCTOR visibility:public <>
(newsSources:kotlin.collections.Set<com...kotlinbase.database.entity.interfaces.NewsSource>)
returnType:com...kotlinbase.database.entity.interfaces.GraphUnit
[primary]

可行但冗余的实现方案

改用以下写法可正常运行,但代码不够简洁:

abstract class GraphUnit: NewsParent {
    //...
    abstract fun getNewsSources():Set<NewsSource>
    fun getImportantNewsCompleted(): List<Informacion> {
       val newsSources = getNewsSources()
       //...
    }
}

@Entity
class Escenario (
    @OneToMany(mappedBy = "escenario")
    val etiquetas: Set<Etiqueta>
): GraphUnit(), NewsSource {
    override fun getNewsSources(): Set<NewsSource> {
        return etiquetas
    }
}

疑问

曾找到讨论相同错误的帖子,但该问题场景与自身不符——本人从未使用过no-arg插件,特此请教该错误的原因及解决方案。

内容的提问来源于stack exchange,提问作者Lucas Tomic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:23:24