Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Jpa Data , Pageable , Pagerequest

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:

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).....

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

My @Repository class:

@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:

@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.

like image 955
Resul Rzaeeff Avatar asked Jan 30 '17 07:01

Resul Rzaeeff


People also ask

What is PageRequest in spring?

Method SummaryReturns the Pageable requesting the first page. Returns the sorting parameters. Returns the Pageable requesting the next Page .

What is Pageable unpaged ()?

spring-projects-issues commented on Mar 19, 2020 unpaged() . means that invoking repository. findAll(Pageable. unpaged()) should load all entities.

How does JPA pagination works internally?

Pagination is a simple but important feature to limit the size of your result set to a number of records that can get efficiently processed by your application and the user. You can configure it with JPA and Hibernate by calling the setFirstResult and setMaxResults on the Query or TypedQuery interface.

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.


2 Answers

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

For example:

PageRequest page_req = new PageRequest(0, last_item, Direction.DESC, "idNode");
like image 102
Jakub Ch. Avatar answered Oct 21 '22 06:10

Jakub Ch.


PageRequest constructors are deprecated. Use the PageRequest.of instead.

public static PageRequest of(int page, int size) {
    return of(page, size, Sort.unsorted());
}

public static PageRequest of(int page, int size, Sort sort) {
    return new PageRequest(page, size, sort);
}

public static PageRequest of(int page, int size, Direction direction, String... properties) {
    return of(page, size, Sort.by(direction, properties));
}
like image 42
mukherjeerajdeep Avatar answered Oct 21 '22 04:10

mukherjeerajdeep