Лучшая практика избавиться от LazyInitializationException?

У меня есть @async (updateSeikoFile(automatedCentring)) метод который использует Contact. Но после маппинга, контакт имеет только свой ID, все остальное null;

@Override
public ResponseEntity<AutomatedCentringDTO> createAutomatedCentring(final AutomatedCentringDTOCreate dto) {
    var automatedCentring = automatedCentringMapper.map(dto);
    automatedCentring = automatedCentringService.create(automatedCentring);
    return ResponseEntity.ok(automatedCentringMapper.map(automatedCentring));
}

AutomatedCentringMapper:

@Mapping(target = "contact", expression = "java(dto.getContactId()!=null?cloud.project.server.model.contacts.Contact.builder().id(dto.getContactId()).build():null)")
AutomatedCentring mapDtoCreateToModel(final AutomatedCentringDTOCreate dto);

Service:

public AutomatedCentring create(@NotNull AutomatedCentring automatedCentring) {
    automatedCentring = automatedCentringRepository.save(automatedCentring);

    //********************************************************************
    // ASYNC SEND UPDATED FILE TO SEIKO
    // Lazy loading of related entities
    //********************************************************************
    automatedCentring.getContact().hashCode();
    entityManager.detach(automatedCentring);

    // async method
    updateSeikoFile(automatedCentring);

    return automatedCentring;
}

AutomatedCentring:

public class AutomatedCentring extends AbstractEntity {

@NotNull(message = "{automatedcentring.contact.required}")
@JoinColumn(name = "contact_id")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Contact contact;
}

То что я заметил, если прикрепить объект контакт к AutomatedCentring после созранения в дб, то контакт не будет null;


Ответы (0 шт):