findAllBy JPA with embeddedId



我有这个Document休眠的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>

有没有其他方法可以为这种方法起一个更好的名字?

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

@EmbeddedId
@NotNull
val documentExpertKey: DocumentExpertKey,
@Column(name = "expertId", nullable = false)
val id: String,

这样您的查询就可以:

fun findAllByDocumentExpertKeyId(String expertId): List<Document>

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

最新更新