Skip to content

Commit

Permalink
Fix case document lazy loading (#1331)
Browse files Browse the repository at this point in the history
Lazy load the case defendants documents to fix the error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: uk.gov.justice.probation.courtcaseservice.jpa.entity.CaseDefendantEntity.documents: could not initialize proxy - no Session
  • Loading branch information
Obsiye authored Feb 3, 2025
1 parent 4202d47 commit a4838ad
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.probation.courtcaseservice.controller.mapper;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.Hibernate;
import org.jetbrains.annotations.NotNull;
import uk.gov.justice.probation.courtcaseservice.controller.model.CaseCommentResponse;
import uk.gov.justice.probation.courtcaseservice.controller.model.CaseDocumentResponse;
Expand Down Expand Up @@ -47,7 +48,10 @@ public static CourtCaseResponse mapFrom(HearingEntity hearingEntity, String defe

private static List<CaseDocumentResponse> mapCaseDocuments(HearingEntity hearingEntity, String defendantId) {
return hearingEntity.getCourtCase().getCaseDefendant(defendantId)
.map(CaseDefendantEntity::getDocuments)
.map(caseDefendantEntity -> {
Hibernate.initialize(caseDefendantEntity.getDocuments());
return caseDefendantEntity.getDocuments();
})
.map(caseDefendantDocumentEntities -> caseDefendantDocumentEntities.stream()
.map(doc -> new CaseDocumentResponse(doc.getDocumentId(),doc.getCreated(), new CaseDocumentResponse.FileResponse(doc.getDocumentName(), 0)))
.collect(Collectors.toList())
Expand Down

0 comments on commit a4838ad

Please sign in to comment.