Lazy loading clob fields of Entites using Hibernate
This is an efficient approach to load clob fields lazily of an Entity. Using this approach,
- distinct operation over the entities can be executed.
- avoid (n + 1) queries while fetching the clob fields.
How to
- Maintain two separate Entities (one is with clob field, another one is without clob field) pointing to the same database table.
- In service layer, fetch the entities according to the search parameters without clob field. As clob field is not considering in this entity, We can run distinct operation if it is needed.
- We can get the ids of the entities fetched in step 2. Afterwards, we can fetch the entities with clob field by the ids in the same persistent context.
@Transactional(readOnly = true)
public Page
getNewsWithContent(Map
parameters, Pageable page) {
// Fetch the news (without content clob) using the search parameters
// run distinct if we want to fetch distinct news
String title = parameters.getOrDefault("title", null);
Specification
distinctNewsSpecification =
Specification.where(NewsSpecification.titleEquals(title))
.and(NewsSpecification.distinct());
Page
pagedNews = newsRepository.findAll(distinctNewsSpecification, page);
// get the news ids which are fetched
List
newsIds = pagedNews.getContent().stream().map(news -> news.getNewsId()) .collect(Collectors.toList()); // fetch the news (with content clob) of the news ids Specification
newsWithContentSpecification = Specification.where(NewsWithContentSpecification.newsIdIn(newsIds)); List
newsWithContent = newsWithContentRepository.findAll(newsWithContentSpecification); return new PageImpl
(newsWithContent, pagedNews.getPageable(), pagedNews.getTotalElements()); }