Home Spring Data Projection size()
Reply: 0

Spring Data Projection size()

user1162 Published in May 21, 2018, 12:51 pm

Is there a way to return the size of a collection via rest api projection?

Consider this example:

The data:

public class MyData {
    // id 
    // ...

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "mydata")
    private final Set<User> users = new HashSet<>();
    // getters/setters ...

the repository:

public interface MyDataRepository extends PagingAndSortingRepository<MyData, Long> {

the projection:

@Projection(name = "short", types = {MyData.class})
public interface MyDataProjection {
    // neither of those work
    // @Value("#{target.getUsers().size()}")
    Integer nrUsers();

I want to get the number of Users in a MyData-Object returned via REST api.

For example: my-domain/my-service/mydatas/123/?projection=short

should return:

    "nrUsers": 4;

Is it possible anyway?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301867 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO