【问题标题】:Override Pageable findAll for selecting fewer columns in Spring Data Rest覆盖 Pageable findAll 以在 Spring Data Rest 中选择更少的列
【发布时间】:2014-10-23 04:18:48
【问题描述】:

当转到 spring data rest 中从 /api 页面发现的页面时,如何覆盖 spring 数据存储库以仅选择选定的列。

我添加 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!"}

【问题讨论】:

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


    【解决方案1】:

    我在与User 相同的包中创建了一个UserSummaryProjection

    @Projection(name = "summary", types = User.class)
    public interface UserSummaryProjection {
    
        Integer getId();
    
        String getEmail();
    
    }
    

    然后,在 /api/users/users/3?projection=summary 进行操作会给我想要的结果,而无需更改存储库。

    【讨论】:

      【解决方案2】:

      选择 User 的子元素并仍然创建 User 有点违反直觉。

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

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

      并基于这个新实体创建一个存储库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-05
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多