Skip to content

Commit

Permalink
Avoid Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
PaRangger committed Nov 28, 2024
1 parent 17eb6ce commit a691219
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -257,8 +258,15 @@ private GroupChatDTO convertGroupChatToDto(User requestingUser, GroupChat groupC
@NotNull
private Set<ConversationParticipant> getConversationParticipants(Conversation conversation) {
Set<ConversationParticipant> 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();
}
Expand Down

0 comments on commit a691219

Please sign in to comment.