8 Mart 2018 Perşembe

SpringData PagingAndSortingRepository Arayüzü

Giriş
Şu satırı dahil ederiz.
import org.springframework.data.repository.PagingAndSortingRepository;
Tanımlama
Şöyle yaparız.  Spring'in kuralların uygun yazılan metod imzaları için Spring kod üretir.
@Repository
public interface CartRepository extends PaginAndSortingRepository<Cart, Long>
{
  ...
}
findAll metodu
Eğer EAGER ilişki varsa veritabanına maliyeti yüksek. Açıklaması şöyle.
When you want to create a paged query that contains information from one or more relationships, loading the data belonging to relationships directly from the select query is a (very) bad idea. For instance, when we retrieve the first page of the articles with their topics, it is better not to load directly all the articles+topics data, but first load the data from the articles and then the data related to the topics.

Indeed, without this, the application will be forced to recover the full data set of the join between the 2 tables, store them in memory, then select only the data of the requested page. This is directly related to the way databases work: they must select all the data of the join even if we only need a fragment of the data (a page, a min/max or the top x of the results).

In the case of a loading a page with its relationships, an explicit message appears in the logs to warn you:

HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!

The greater the volume of the 2 tables, the higher the impact: with tables containing millions of entries, this can result in extremely expensive processing for the application.
Eper LAZY ilişki varsa bu sefer de "Hibernate N+1 query problem" ile karşılaşıyoruz. LAZY ilişkide çözümlerden birisi Hibernate'in @BatchSize(size = n) anotasyonunu kullanmak.


Hiç yorum yok:

Yorum Gönder