-3

I want to return "ownDemand" as Page of demand. How can I do that

@Override
public Page<Demand> getDemandbyId(Long id, Pageable pageable) {
   Iterable<Demand> alldemand = demandRepository.findAll(); 
   List<Demand> ownDemand = new ArrayList<Demand>();
   for (Demand demand : alldemand) {
       if(demand.getStore().getId()==id) {
           ownDemand.add(demand);
       }
  }
    return null;
 }
 }

Demand Repository

@RestResource
public interface DemandRepository extends JpaRepository<Demand,Long> {
}
  • 1
    Please add your `DemandRepository` and entity class like `Demand`. And you can call findAll with pageable method – Eklavya Aug 09 '20 at 20:16
  • @User-Upvotedon'tsayThanks, But I dont want return findAll I want return demands of an store that have the id whan I instanciate Page = new Page() and other method(sort number page ....) that I dont know how – junior developer Aug 09 '20 at 20:26
  • 1
    Don't, please don't. This will lead to issues when you have a lot of `Demand` instances (memory, performance). Just write a proper query(method) for it. Something like `Page findAllByStoreId(Long storeId, Pageable page)`; which will return exactly what you need AND uses the power of the database instead of pulling everythingi n memory and do paging and filtering. – M. Deinum Aug 10 '20 at 06:32

2 Answers2

0

Why not add a method to your DemandRepository like so

Page<Demand> findAllByStore(Store store, Pageable page)

This assumes that StoreEntity is related to Demand, of course.

Prashant
  • 1,002
  • 13
  • 29
-1

Try below:

demandRepository.findAll(PageRequest.of(0, 2))

By signature it returns Page. Change PageRequest according to your fit.

Suman
  • 818
  • 6
  • 17
  • But I dont want return findAll I want return demands of an store that have the id whan I instanciate Page = new Page() and other method(sort number page ....) that I dont know how – junior developer Aug 09 '20 at 20:35
  • Check this. https://stackoverflow.com/questions/37749559/conversion-of-list-to-page-in-spring Looks like your issue is duplicate of this. – Suman Aug 09 '20 at 20:45