findAllBy JPA with embeddedId

问题描述 投票:1回答:1

我有这个Document hibernate jpa实体与EmbeddedId

@Entity
data class Document(
        @EmbeddedId
        @NotNull
        val documentExpertId: DocumentExpertId,
        // other fields
)

@Embeddable
data class DocumentExpertId(

        @Column(nullable = false)
        val expertId: String,

        @Column(nullable = false)
        val name: String

) : Serializable

要通过expertId获取所有文档,我想让我的文档JPA存储库接口方法调用:

fun findAllByExpertId(String expertId): List<Document>

但是,我发现这样做的唯一方法是:

fun findAllByDocumentExpertIdExpertId(String expertId): List<Document>

还有另一种方法可以为这种方法提供更好的名称吗?

kotlin spring-data-jpa jpql composite-primary-key hibernate-jpa
1个回答
0
投票

您可以将ID和列定义更改为:

    @EmbeddedId
    @NotNull
    val documentExpertKey: DocumentExpertKey,

    @Column(name = "expertId", nullable = false)
    val id: String,

这样您的查询可能是:

    fun findAllByDocumentExpertKeyId(String expertId): List<Document>

这对我来说看起来更正常。

© www.soinside.com 2019 - 2024. All rights reserved.