我有一个控制器可以在我的数据库中对所有新闻进行分页和排序:
@RequestMapping(value = "/viewstatus", method = RequestMethod.GET)
ModelAndView viewStatus(ModelAndView modelAndView, @RequestParam(name = "p", defaultValue = "1") int pageNumber) {
Page<StatusUpdate> page = statusUpdateService.getPage(pageNumber);
modelAndView.getModel().put("page", page);
modelAndView.setViewName("app.viewStatus");
return modelAndView;
}
通过调用可以正常工作的服务:
public Page<StatusUpdate> getPage(int pageNumber) {
PageRequest request = new PageRequest(pageNumber-1, pageSize, Sort.Direction.DESC, "added");
return statusUpdateDao.findAll(request);
}
但是现在,我想用一个参数(SiteUser)做同样的SortingAndPaging BUT。这是我的目标:
@Entity
@Table(name = "status_update")
public class StatusUpdate {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min=5, max=255, message="{addstatus.title.size}")
@Column(name = "title")
private String title;
@Size(min=5, max=5000, message="{addstatus.text.size}")
@Column(name = "text")
private String text;
@Column(name = "added")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss")
private Date added;
@OneToOne(targetEntity = SiteUser.class)
@JoinColumn(name="user_id")
private SiteUser siteUser;
@PrePersist
protected void onCreate() {
if (added == null) {
added = new Date();
}
}
public StatusUpdate() {
}
但是当我这样做时,它给了我这个错误:
Exception: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [3] did not match expected type [com.caveofprogramming.model.entity.SiteUser (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [3] did not match expected type [com.caveofprogramming.model.entity.SiteUser (n/a)]
Failed URL: http://192.168.160.128:8080/viewmystatus
Exception message: Parameter value [3] did not match expected type [com.caveofprogramming.model.entity.SiteUser (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [3] did not match expected type [com.caveofprogramming.model.entity.SiteUser (n/a)]
我厌倦了发送一个字符串,但它没有用。我不得不将其更改为NativeQuerry,但我不使用PagingAndSorting,它只能用作List(这是一个令人头疼的工作)。所以,如果你能帮助我解决PagingAndSorting的错误,那就太棒了。
这是控制器:
@RequestMapping(value = "/viewmystatus", method = RequestMethod.GET)
ModelAndView viewMyStatus(ModelAndView modelAndView, @RequestParam(name = "p", defaultValue = "1") int pageNumber) {
SiteUser user = getUser();
Long user_id= user.getId();
Page<StatusUpdate> page = statusUpdateService.findMyStatusUpdates(user_id, pageNumber);
for(StatusUpdate statusUpdate: page){
SiteUser siteUser= statusUpdate.getSiteUser();
modelAndView.getModel().put("siteuser", siteUser);
}
modelAndView.getModel().put("page", page);
modelAndView.setViewName("app.viewStatus");
return modelAndView;
}
这是服务:
public Page<StatusUpdate> findMyStatusUpdates(Long user_id, int pageNumber) {
PageRequest request = new PageRequest(pageNumber-1, pageSize, Sort.Direction.DESC, "added");
return statusUpdateDao.findBySiteUser(user_id, request);
}
和DAO:
@Repository
public interface StatusUpdateDao extends PagingAndSortingRepository<StatusUpdate, Long> {
StatusUpdate findFirstByOrderByAddedDesc();
Page<StatusUpdate> findBySiteUser(Long user_id, Pageable pageable);
}
感谢您的帮助!
答案 0 :(得分:0)
使用SiteUser
对象:
public Page<StatusUpdate> findMyStatusUpdates(Long user_id, int pageNumber) {
PageRequest request = new PageRequest(pageNumber-1, pageSize, Sort.Direction.DESC, "added");
return statusUpdateDao.findBySiteUser(new SiteUser(user_id), request);
}
您也可以尝试按嵌套属性进行查询,但我认为您必须将user_id
更改为userId
,因为下划线是保留字符。看一下文档:
Spring data JPA Property expressions
您是否更改了findBySiteUser
方法?你发布的那个看起来很好但似乎抱怨签名。即使PageRequest
实现Pageable
,也必须使用Pageable
明确声明签名,但您收到的错误消息显示PageRequest
public abstract org.springframework.data.domain.Page com.caveofprogramming.model.repository.StatusUpdateDao.findBySiteUser(com.caveofprogramming.model.entity.SiteUser,org.springframework.data.domain.PageRequest)
更多信息:
PageRequest parameter not recognized as Pageable in Paging query