I\'m trying to get a page of a partial entity (NetworkSimple) using the new feature of spring data, projections
I\'ve checked the documentation and if I request only:
Note: This feature should work in the way described by the original poster but due to this bug it didn't. The bug has been fixed for the Hopper SR2 release, if you're stuck on an earlier version then the workaround below will work.
It is possible to use Pageable
with the new query projection features introduced in Spring Data JPA 1.10 (Hopper). You will need to use the @Query
annotation and manually write a query for the fields you require, they must also be aliased using AS
to allow Spring Data to figure out how to project the results. There is a good example in spring-boot-samples part of the spring boot repository.
In your example it would be quite simple:
@Query("SELECT n.id AS id, n.name AS networkName, n.active AS isActive FROM Network n")
Page findAllProjectedBy(Pageable pageable);
I have made the assumption that your entity looks something like this:
@Entity
public class Network
{
@Id
@GeneratedValue
private Long id;
@Column
private String name;
@Column
private boolean active;
...
}
Spring Data will derive a count query automatically for the paging information. It is also possible to make use of joins in the query to fetch associations and then summarise them in the projection.