Я предлагаю вам не смешивать модель данных и представление (ответ сущности и http в вашем случае).
По умолчанию вы можете оставить объект Job
с отложенной загрузкой, но не используйте его в качестве ответа службы покоя. Создайте отдельный класс, который будет представлять HTTP-ответ и переносить данные из вашей сущности. Например:
@Table(name = "JOB")
public class Job implements Serializable {
@Id
@Column(name = "JOB_ID", unique = true, nullable = false)
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "job")
@Column(name = "PRINT_JOB_ID", length = 30)
private JobDocument jobDocuments;
. . .
}
// your response class, which wrap Job data
public class JobResponse {
@JsonProperty("id")
private Long id;
@JsonProperty("jobDocuments")
private JobDocument jobDocuments
. . .
// use this when you need to have jobDocuments
public static JobResponse fromJobWithDocuments(Job job) {
this.id = job.getId();
this.jobDocuments = job.getJobDocuments(); // you fetch lazy field, so it would be pre-populated
}
// use this when you don't need to have jobDocuments
public static JobResponse fromJob(Job job) {
this.id = job.getId();
}
}
И предположим, у вас есть такой контроллер:
public class Controller {
. . .
public ResponseEntity<JobResponse> getJob(boolean withDocuments, long jobId) {
JobResponse response;
Job job = jobService.getJob(jobId); // assuming you are getting job somehow
if (withDocuments) {
response = JobResponse.fromJobWithDocuments(job)
} else {
response = JobResponse.fromJob(job)
}
return new ResponseEntity<JobResponse>(response);
}
. . .
}