Java Spring Jpa 数据、Pageable、Pagerequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41930627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:47:39  来源:igfitidea点击:

Spring Jpa Data , Pageable , Pagerequest

javaspringspring-mvcspring-data-jpa

提问by Resul Rzaeeff

I am using Spring JPA DATA in my project. I wanna make my data pageble. I have already add Pageable to my repository method. But it shows error something like that:

我在我的项目中使用 Spring JPA DATA。我想让我的数据分页。我已经将 Pageable 添加到我的存储库方法中。但它显示了类似的错误:

Request processing failed; nested exception is java.lang.IllegalArgumentException: You have to provide at least one property to sort by! java.lang.IllegalArgumentException: You have to provide at least one property to sort by! org.springframework.data.domain.Sort.<init>(Sort.java:92) org.springframework.data.domain.Sort.<init>(Sort.java:80) org.springframework.data.domain.PageRequest.<init>(PageRequest.java:52) com.datum.fnd.controller.rest.NodeRestController.getNodes(NodeRestController.java:44) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method).....

请求处理失败;嵌套异常是 java.lang.IllegalArgumentException:您必须至少提供一个属性进行排序!java.lang.IllegalArgumentException:您必须至少提供一个属性进行排序!org.springframework.data.domain.Sort.<init>(Sort.java:92) org.springframework.data.domain.Sort.<init>(Sort.java:80) org.springframework.data.domain.PageRequest。 <init>(PageRequest.java:52) com.datum.fnd.controller.rest.NodeRestController.getNodes(NodeRestController.java:44) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method).....

I want to share my codes with you. Please look over following codes. I haven't find where I did mistake

我想和你分享我的代码。请查看以下代码。我还没有找到我做错的地方

My @Repository class:

我的@Repository 类:

@Query(value = "select new com.datum.fnd.domain.Node(c.idNode, c.name,c.address, c.description, c.point) from Node c")
List<Node> getNodesByPage(Pageable pageable);

My Service class:

我的服务类:

public interface NodeService extends DefaultService<Node, Long> {
    List<Node> getUnboundedNodes();

    List<Node> getNodes(Pageable pageable);
}

My ServiceImpl class:

我的 ServiceImpl 类:

@Service
public class NodeServiceImpl implements NodeService {

private static final Logger LOG = Logger.getLogger(NodeServiceImpl.class);

@Autowired
private NodeRepository nodeRepository;

@Autowired
private ChannelNodeRepository channelNodeRepository;

@Override
public Node create(Node node) {
    return nodeRepository.save(node);
}


@Transactional(readOnly = true)
@Override
public List<Node> list() {
    return nodeRepository.selectAll();
}

@Transactional(readOnly = true)
@Override
public List<Node> getNodes(Pageable pageable) {
    return nodeRepository.getNodesByPage(pageable);
}

My Controller class:

我的控制器类:

@RequestMapping(value = "/page/{last_item}", method = RequestMethod.GET)
public List<Node> getNodes(@PathVariable(value = "last_item") int last_item) {
    LOG.info("Retrieve all nodes");

    PageRequest page_req = new PageRequest(0, last_item, Direction.DESC);
    return nodeService.getNodes(page_req);
}

And when I try to call "http://localhost:8088/FNDWEB/rest/node/page/50" it shows the above-mentioned error.

当我尝试调用“ http://localhost:8088/FNDWEB/rest/node/page/50”时,它显示了上述错误。

采纳答案by Jakub Ch.

Exception message and documentation of PageRequestshould make your problem clear. If you provide Sort.Directionto PageRequestconstructor, you have to provide also at least one property name (according to which entities should be sorted).

PageRequest 的异常消息和文档应该使您的问题清楚。如果您提供Sort.DirectionPageRequest构造函数,则还必须至少提供一个属性名称(根据应排序的实体)。

For example:

例如:

PageRequest page_req = new PageRequest(0, last_item, Direction.DESC, "idNode");