Issue
I have an entity with the following relationship in my Store entity:
@OneToMany(mappedBy="computer")
@JsonProperty("computers")
public Set<Computer> computers;
In my StoreService:
List<Computers> computers;
Pageable paging = PageRequest.of(page, size);
Page<Computers> pageTuts;
pageTuts = storeRepository.findAllComputers(paging);
computers = pageTuts.getContent();
Map<String, Object> response = new HashMap<>();
response.put("computers", computers);
response.put("current_page", pageTuts.getNumber());
response.put("total_items", pageTuts.getTotalElements());
response.put("total_pages", pageTuts.getTotalPages());
Now I need to somehow in my StoreRepository interface query for all computers that are relational to that entity. How can I do that? I thought I could just add a name like this:
Page<Computers> findAllComputers(Pageable pageable);
Any ideas how to solve this? Do I have to write a custom Query or something? I think this operation should be some kind of standard so it's hard to think that I would need that.
Solution
You should create ComputerRepository
and add this kind of method to your repository class.
public interface ComputerRepository extends PagingAndSortingRepository<Computer, Integer> {
Page<Computers> findByStore(Store store, Pageable pageable);
}
Here is quick explanation of Pagination and Sorting.
https://www.baeldung.com/spring-data-jpa-pagination-sorting
Answered By - Alper Derya
Answer Checked By - Senaida (JavaFixing Volunteer)