Spring Data Projections

时间:2018-05-13 13:38:11

标签: spring kotlin spring-data spring-data-jpa

我有一个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

但它没有识别默认方法实现。

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

1 个答案:

答案 0 :(得分:0)

您无法在Spring注释中使用count,因为它不是真正的Collections方法;这是扩展功能

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

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

相关问题