覆盖Pageable findAll,用于在Spring Data Rest中选择较少的列

时间:2014-10-23 04:18:48

标签: java spring spring-data spring-data-rest

如何覆盖spring数据存储库,以便在转到从spring数据休息的/ api页面中发现的页面时仅选择所选列。

我添加了findAll,如下所示 -

public interface UserRepository extends BaseRepository<User, Integer>, UserRepositoryCustom {

    @Query("select u from User u where email = :email and password = :password")
    @Cacheable(value = "user-cache", key = "#user.login")
    @RestResource(exported = false)
    public User findUserByEmailAndPassword(@Param("email") String email, @Param("password") String password);

    @RestResource(rel = "byEmail", path = "byEmail")
    public User findUserByEmail(@Param("email") String email);

    @RestResource(rel = "byPhone", path = "byPhone")
    public User findUserByPhone(@Param("phone") String phone);

     @Override 
     @Query("select u.id,u.email,u.phone from User u ")
     public Page<User> findAll(Pageable pageable);
}

/ api / users发出错误 -

{"cause":null,"message":"PersistentEntity must not be null!"}

2 个答案:

答案 0 :(得分:3)

我在与UserSummaryProjection

相同的包中创建了一个User
@Projection(name = "summary", types = User.class)
public interface UserSummaryProjection {

    Integer getId();

    String getEmail();

}

然后,进入/api/users/users/3?projection=summary可以在不更改存储库的情况下获得所需的结果。

答案 1 :(得分:0)

选择用户的子元素并仍在创建用户有点违反直觉。

我会创建另一个实体,例如 UserDetails ,它将由具有相同映射的同一个表映射。

public class UserDetails {
    private int uid;
    private String email;
    private String phone;
}

根据这个新实体创建一个存储库。