我在Spring Data Jpa和基于Spring Data REST的应用程序的资源,服务和存储库中有以下代码:
在Resource.java中,
// Get User Assigned Customers by User Id
@RequestMapping(value = "/userAssignedClientss/byUserId",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<UserAssignedClients>> findByUserId(@RequestParam(value="userId") Long userId,
@RequestParam(value = "page" , required = false) Integer offset,
@RequestParam(value = "per_page", required = false) Integer limit ) throws URISyntaxException {
log.debug(" REST request to get User Assigned Clients By User Id " + userId);
Page<UserAssignedClients> page = userAssignedClientsService.findByUserId(userId, offset, limit);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/userAssignedClientss/byUserId", offset, limit);
return new ResponseEntity<List<UserAssignedClients>>(page.getContent(), headers, HttpStatus.OK);
}
在我的Service.java中:
public Page<UserAssignedClients> findByUserId(Long userId, Integer offset, Integer limit) {
Pageable pageable = new PageRequest(offset, limit, Sort.Direction.ASC, "userId");
Page<UserAssignedClients> userAssignedClientsList = userAssgClientsRepository.findByUserId(userId, pageable);
log.debug(" - Returned userAssignedCustomersList Size " + userAssignedClientsList.getSize());
return userAssgClientsRepository.findByUserId(userId, pageable);
}
在Repository.java中:
Page<UserAssignedClients> findByUserId(Long userId, Pageable pageable);
我原本期望查询形成类似于select * from user_assigned_clients where user_id = ?
但令人惊讶的是count(*)查询正在形成,我是空数组作为响应。以下是我在eclipse中的java调试日志。
[DEBUG] com.sample.aop.logging.LoggingAspect - Enter: com.sample.web.rest.UserAssignedClientsResource.findByUserId() with argument[s] = [123456, 1, 10]
[DEBUG] com.sample.web.rest.UserAssignedClientsResource - : REST request to get User Assigned Clients By User Id 123456
[DEBUG] com.sample.aop.logging.LoggingAspect - Enter: com.sample.service.UserAssignedClientsService.findByUserId() with argument[s] = [123456, 1, 10]
Hibernate: select count(userassign0_.id) as col_0_0_ from user_assigned_clients userassign0_ where userassign0_.user_id=?
[DEBUG] com.sample.service.UserAssignedClientsService - - Returned userAssignedCustomersList Size 10
Hibernate: select count(userassign0_.id) as col_0_0_ from user_assigned_clients userassign0_ where userassign0_.user_id=?
[DEBUG] com.sample.aop.logging.LoggingAspect - Exit: com.sample.service.UserAssignedClientsService.findByUserId() with result = Page 1 of 1 containing UNKNOWN instances
[DEBUG] com.sample.aop.logging.LoggingAspect - Exit: com.sample.web.rest.UserAssignedClientsResource.findByUserId() with result = <200 OK,[],{X-Total-Count=[1], Link=[</userAssignedClientss/byUserId?page=1&per_page=10>; rel="last",</userAssignedClientss/byUserId?page=1&per_page=10>; rel="first"]}>
以下是我的UserAssignedClients.java fie:
/**
* A UserAssignedClients.
*/
@Entity
@Table(name = "user_assigned_clients")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class UserAssignedClients implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "user_id")
private Long userId;
@Column(name = "user_code")
private String userCode;
@Column(name = "client_id")
private Long clientId;
@Column(name = "client_code")
private String clientCode;
@Column(name = "client_name")
private String clientName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public Long getClientId() {
return clientId;
}
public void setClientId(Long clientId) {
this.clientId = clientId;
}
public String getClientCode() {
return clientCode;
}
public void setClientCode(String clientCode) {
this.clientCode = clientCode;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UserAssignedClients userAssignedClients = (UserAssignedClients) o;
if ( ! Objects.equals(id, userAssignedClients.id)) return false;
return true;
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return "UserAssignedClients{" +
"id=" + id +
", userId='" + userId + "'" +
", userCode='" + userCode + "'" +
", clientId='" + clientId + "'" +
", clientCode='" + clientCode + "'" +
", clientName='" + clientName + "'" +
'}';
}
}
答案 0 :(得分:0)
这是因为您正在请求Pageable返回值(您使用Page对象返回)。 Spring JPA Data推测您期望获得多个结果,并且由于您要求使用Pagaeble对象,它首先要做的是尝试获取需要“分页”的记录总数。
如果您阅读documentation的这一部分,您会看到:
第一种方法允许你传递 org.springframework.data.domain.Pageable实例到查询方法 动态地向您的静态定义查询添加分页。 一页 了解可用元素和页面的总数。确实如此 所以通过基础设施触发计数查询来计算 总数。
如果您希望返回一些客户端,那么您仍然需要弄清楚为什么count
返回0,这意味着它没有找到该userId的Clients
。但这是一个不同的问题。