Reputation: 429
I have a User
entity with plenty of fields (i omit the code because it is very long and i do not think it's important in this case, let me know if you need it).
This is part of my repository:
interface UserRepository: JpaRepository<User, String>{
fun findByNameContainingIgnoreCase(name: String, page: Pageable): Page<UserProfile>?
}
Now, I defined the following projection:
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>?
}
The problem is that i do not want to return the list of user documents, but the number of his documents. I tried with:
@Value("#{target.documents?.count}")
fun getDocumentsCount(): Int?
but it doesn't recognize the count field.
I also tried with:
@JsonIgnore
fun getDocuments(): MutableSet<Document>?
fun getDocumentsCount(): Int? = getDocuments()?.size
but it doesn't recognize the default method implementation.
I don't know what else to try. Can anyone help me? Thanks a lot!
Upvotes: 4
Views: 1655
Reputation: 1467
You're not going to be able to use count
in a Spring annotation because it's not an actual Collections method; it's an extension function.
You need to combine your two existing solutions. Try it with the Value annotation, but using the right MutableSet
function:
@Value("#{target.documents?.size}")
Upvotes: 2