O
O
Orkhan Hasanli2020-08-20 22:20:51
Java
Orkhan Hasanli, 2020-08-20 22:20:51

Why can there be a long response from the server in Spring REST Repositories?

Hello!
There is a Spring Boot application. Connected Spring Rest Repositories. The data is stored in a database (MySQL).
There are 2 entities:

Author {
@JsonIgnore
@OneToMany(mappedBy = "author")
List<Book> books = new ArrayList();
}

Book {
@JsonIgnore @ManyToOne
  @JoinColumn(name = "author_id")
  private Author author;
}


Here is the repository:
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {

  List<Book> findAllByAuthor(Author author);
  Page<Book> findAllByAuthor(Author author, Pageable pageable);

}

Added to the config file:
spring.data.rest.basePath=/api
spring.data.rest.defaultPageSize=100


If you go to the author url
http://localhost:7777/api/authors/1/books
and request books by this author, and if there are a lot of books, then the request takes a long time to complete. It seems to me that he is trying to return all the books of this author (about 500,000) instead of returning them page by page

Why is this happening and how to fix it?

Here are the screenshots:
5f3ee1720e02d522525060.png
5f3ee18516b53895958042.png
And then when you go to the url
http://localhost:7777/api/cities/4/features
or
http://localhost:7777/api/cities/4/features?page=1
an already long request

Here is the log in show_sql when accessing the above urls:
Hibernate: select city0_.city_id as city_id1_0_0_, city0_.city_iso_code as city_iso2_0_0_, city0_.city_name as city_nam3_0_0_, city0_.country_id as country_4_0_0_, country1_.country_id as country_1_1_1_, country1_.country_iso_code as country_2_1_1_, country1_.country_name as country_3_1_1_, country1_.map_link as map_link4_1_1_ from cities city0_ left outer join countries country1_ on city0_.country_id=country1_.country_id where city0_.city_id=?
Hibernate: select features0_.city_id as city_id6_4_0_, features0_.feature_id as feature_1_4_0_, features0_.feature_id as feature_1_4_1_, features0_.city_id as city_id6_4_1_, features0_.city_iso_code as city_iso2_4_1_, features0_.country_id as country_7_4_1_, features0_.country_iso_code as country_3_4_1_, features0_.type as type4_4_1_, features0_.wkt as wkt5_4_1_, country1_.country_id as country_1_1_2_, country1_.country_iso_code as country_2_1_2_, country1_.country_name as country_3_1_2_, country1_.map_link as map_link4_1_2_ from features features0_ left outer join countries country1_ on features0_.country_id=country1_.country_id where features0_.city_id=?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2020-08-20
@Jacen11

do pagination

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question