From a6912194c3c04c9decf50b96a43dd63c0cb1d6da Mon Sep 17 00:00:00 2001 From: Paul Rangger Date: Thu, 28 Nov 2024 15:54:38 +0100 Subject: [PATCH] Avoid Exception --- .../service/conversation/ConversationDTOService.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/communication/service/conversation/ConversationDTOService.java b/src/main/java/de/tum/cit/aet/artemis/communication/service/conversation/ConversationDTOService.java index 6dd78aa23098..6d9831036903 100644 --- a/src/main/java/de/tum/cit/aet/artemis/communication/service/conversation/ConversationDTOService.java +++ b/src/main/java/de/tum/cit/aet/artemis/communication/service/conversation/ConversationDTOService.java @@ -9,6 +9,7 @@ import jakarta.persistence.Persistence; import jakarta.validation.constraints.NotNull; +import org.hibernate.LazyInitializationException; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @@ -257,8 +258,15 @@ private GroupChatDTO convertGroupChatToDto(User requestingUser, GroupChat groupC @NotNull private Set getConversationParticipants(Conversation conversation) { Set conversationParticipants; - var participantsInitialized = Persistence.getPersistenceUtil().isLoaded(conversation, "conversationParticipants") && conversation.getConversationParticipants() != null - && !conversation.getConversationParticipants().isEmpty(); + boolean participantsInitialized; + try { + participantsInitialized = Persistence.getPersistenceUtil().isLoaded(conversation, "conversationParticipants") && conversation.getConversationParticipants() != null + && !conversation.getConversationParticipants().isEmpty(); + } + catch (LazyInitializationException e) { + // In case the conversation's persistence context was closed already, we need to re-fetch to avoid errors down the line + participantsInitialized = false; + } if (participantsInitialized) { conversationParticipants = conversation.getConversationParticipants(); }