[bugfix]: AI conversation message loading issue #3911
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What's changed?
fixed: #3868
In HertzBeat AI feature, when users switch between multiple conversations, the chat history disappears and only the default welcome page is displayed. This is caused by JPA's
@OneToManylazy loading mechanism that prevents proper data loading.Root Cause Analysis
The core issue lies in JPA's
@OneToManylazy loading behavior:messagesfield inChatConversationentity uses@OneToManyannotation with default lazy loading (FetchType.LAZY) strategyconversationDao.getReferenceById()orfindById(), accessing themessagescollection after Hibernate Session closure triggersLazyInitializationExceptionor returns empty collectionsgetReferenceById()returns a proxy object without immediately triggering database queries, causing associated message data to remain unloadedSolution
Adopt explicit query strategy instead of relying on JPA's automatic association loading:
findByConversationIdOrderByGmtCreateAsc()andfindByConversationIdInOrderByGmtCreateAsc()methods inChatMessageDaofor explicit message queriesgetAllConversations()method, useINquery to load all conversation messages at once, avoiding N+1 query problem@Transactionalannotation todeleteConversation()method to ensure atomicity of delete operationsTesting
20251214-212937.mp4
Checklist
Add or update API