Skip to content

Commit d9e1b63

Browse files
committed
Fixing LazyInitializationException in report retrieval
1 parent 65b7a78 commit d9e1b63

3 files changed

Lines changed: 42 additions & 19 deletions

File tree

roda-common/roda-common-data/src/main/java/org/roda/core/data/v2/jobs/Report.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,11 @@ public String getLineSeparator() {
573573

574574
@JsonIgnore
575575
public Report getLastRunPlugin() {
576-
int size = reports.size();
576+
List<Report> reportsList = getReports();
577+
int size = reportsList.size();
577578
if (size == 0)
578579
return null;
579-
return reports.get(size - 1);
580+
return reportsList.get(size - 1);
580581
}
581582

582583
@Override

roda-core/roda-core/src/main/java/org/roda/core/model/DefaultModelService.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,22 +3038,7 @@ public void deleteJob(String jobId)
30383038
throws NotFoundException, GenericException, AuthorizationDeniedException, RequestNotValidException {
30393039
RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType);
30403040

3041-
boolean deletedFromDb = false;
3042-
3043-
// Try to delete from database first (for running jobs)
3044-
if (isJpaAvailable()) {
3045-
JobRepository jobRepo = getJobRepository();
3046-
ReportRepository reportRepo = getReportRepository();
3047-
if (jobRepo != null && jobRepo.existsById(jobId)) {
3048-
// Delete reports from DB
3049-
if (reportRepo != null) {
3050-
reportRepo.deleteByJobId(jobId);
3051-
}
3052-
// Delete job from DB
3053-
jobRepo.deleteById(jobId);
3054-
deletedFromDb = true;
3055-
}
3056-
}
3041+
boolean deletedFromDb = isDeletedFromDb(jobId);
30573042

30583043
// Also try to delete from storage (for archived jobs or cleanup)
30593044
try {
@@ -3077,6 +3062,26 @@ public void deleteJob(String jobId)
30773062
notifyJobDeleted(jobId).failOnError();
30783063
}
30793064

3065+
private boolean isDeletedFromDb(String jobId) {
3066+
boolean deletedFromDb = false;
3067+
3068+
// Try to delete from database first (for running jobs)
3069+
if (isJpaAvailable()) {
3070+
JobRepository jobRepo = getJobRepository();
3071+
ReportRepository reportRepo = getReportRepository();
3072+
if (jobRepo != null && jobRepo.existsById(jobId)) {
3073+
// Delete reports from DB
3074+
if (reportRepo != null) {
3075+
reportRepo.deleteByJobId(jobId);
3076+
}
3077+
// Delete job from DB
3078+
jobRepo.deleteById(jobId);
3079+
deletedFromDb = true;
3080+
}
3081+
}
3082+
return deletedFromDb;
3083+
}
3084+
30803085
@Override
30813086
public Report retrieveJobReport(String jobId, String jobReportId)
30823087
throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException {

roda-core/roda-core/src/main/java/org/roda/core/repository/job/ReportRepository.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
package org.roda.core.repository.job;
99

1010
import java.util.List;
11+
import java.util.Optional;
1112

1213
import org.roda.core.data.v2.jobs.Report;
14+
import org.springframework.data.jpa.repository.EntityGraph;
1315
import org.springframework.data.jpa.repository.JpaRepository;
1416
import org.springframework.stereotype.Repository;
1517
import org.springframework.transaction.annotation.Transactional;
@@ -24,12 +26,27 @@
2426
public interface ReportRepository extends JpaRepository<Report, String> {
2527

2628
/**
27-
* Find all reports associated with a given job ID.
29+
* Find a report by its ID, eagerly fetching step reports to avoid
30+
* LazyInitializationException when accessed outside a transaction.
31+
*
32+
* @param id
33+
* the report ID
34+
* @return optional containing the report if found
35+
*/
36+
@Override
37+
@EntityGraph(attributePaths = "stepReports")
38+
Optional<Report> findById(String id);
39+
40+
/**
41+
* Find all reports associated with a given job ID, eagerly fetching step
42+
* reports to avoid LazyInitializationException when accessed outside a
43+
* transaction.
2844
*
2945
* @param jobId
3046
* the job ID to search for
3147
* @return list of reports for the specified job
3248
*/
49+
@EntityGraph(attributePaths = "stepReports")
3350
List<Report> findByJobId(String jobId);
3451

3552
/**

0 commit comments

Comments
 (0)