gpt4 book ai didi

Spring 数据预测

转载 作者:行者123 更新时间:2023-12-02 13:00:19 29 4
gpt4 key购买 nike

我有一个包含大量字段的 User 实体(我省略了代码,因为它很长,我认为在这种情况下它不重要,如果您需要它,请告诉我)。

这是我存储库的一部分:

interface UserRepository: JpaRepository<User, String>{
fun findByNameContainingIgnoreCase(name: String, page: Pageable): Page<UserProfile>?
}

现在,我定义了以下投影:

interface UserProfile{
fun getUsername(): String
fun getName(): String
fun getFavoriteTags(): MutableSet<Tag>?
fun getFavoriteDocuments(): MutableSet<Document>?
fun getAverageScore(): Double
fun getUserPicPath(): String
fun getDocuments(): MutableSet<Document>?
}

问题是我不想返回用户文档列表,而是他的文档数。我试过:

@Value("#{target.documents?.count}")
fun getDocumentsCount(): Int?

但它不识别计数字段。

我也尝试过:

@JsonIgnore
fun getDocuments(): MutableSet<Document>?
fun getDocumentsCount(): Int? = getDocuments()?.size

但它不识别默认方法实现。

我不知道还能尝试什么。谁能帮我?非常感谢!

最佳答案

您将无法在 Spring 注释中使用 count,因为它不是实际的 Collections 方法;这是一个扩展函数

您需要结合现有的两个解决方案。尝试使用 Value 注释,但使用正确的 MutableSet 函数:

@Value("#{target.documents?.size}")

关于 Spring 数据预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316722/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com