实体主构造函数中的非空关系

Non null relationships in primary constructor of entity

我正在使用 Spring Data Neo4j 和 Kotlin 创建应用程序。我使用标准的 kotlin 方式来声明实体(class 与主构造函数)。一切正常,直到我想在我的实体之间创建简单的、一对多的强制关系。当我在我的存储库上调用 .findAll() 时,我得到 Parameter specified as non-null is null: method ...model.Campaign.<init>, parameter client

我试图调用 .findAll(depth = 1) 将相关实体加载到我的实体,但这没有帮助。

@NodeEntity
class User(var name: String)
{
    @Id @GeneratedValue
    var id: Long? = null
}

@NodeEntity
class Campaign(
    var name: String,
    @Relationship(type = "CLIENT", direction = Relationship.OUTGOING)
    var client: User)
{
    @Id @GeneratedValue
    var id: Long? = null
}

interface CampaignRepository : Neo4jRepository<Campaign, Long>

//...

campaignRepository.save(Campaign("C1", user))

campaignRespository.findAll()

当然,我可以将 var client: User? 声明为可空,一切都很好。但是,由于在我的模型中我将同时拥有强制关系和可选关系,所以我想知道是否有办法克服这个问题。

我找到了解决方案,但不是很优雅:

@NodeEntity
class Campaign(
    var name: String,
    client: User?)
{
    @Id @GeneratedValue
    var id: Long? = null

    @Relationship(type = "CLIENT", direction = Relationship.OUTGOING)
    lateinit var client: User

    init
    {
        client?.let { this.client = it }
    }
}