P-Peaceful opened a new pull request, #3911:
URL: https://github.com/apache/hertzbeat/pull/3911

   ## What's changed?
   fixed: #3868 
   <!-- Describe Your PR Here -->
   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 `@OneToMany` lazy loading mechanism that prevents 
proper data loading.
   
   ## Root Cause Analysis
   The core issue lies in JPA's `@OneToMany` lazy loading behavior:
   
   1. **Lazy Loading Mechanism**: The `messages` field in `ChatConversation` 
entity uses `@OneToMany` annotation with default lazy loading 
(`FetchType.LAZY`) strategy
   2. **Session Closure Issue**: When fetching conversation objects via 
`conversationDao.getReferenceById()` or `findById()`, accessing the `messages` 
collection after Hibernate Session closure triggers 
`LazyInitializationException` or returns empty collections
   3. **Proxy Object Trap**: `getReferenceById()` returns a proxy object 
without immediately triggering database queries, causing associated message 
data to remain unloaded
   4. **Transaction Boundary**: After Service layer methods return, 
transactions are committed and Hibernate Sessions are closed, preventing 
lazy-loaded associations from being loaded
   
   ## Solution
   Adopt **explicit query strategy** instead of relying on JPA's automatic 
association loading:
   
   1. **Add Dedicated Query Methods**: Add 
`findByConversationIdOrderByGmtCreateAsc()` and 
`findByConversationIdInOrderByGmtCreateAsc()` methods in `ChatMessageDao` for 
explicit message queries
   2. **Manual Message Loading**: Explicitly query and set messages to 
conversation objects where message data is needed
   3. **Batch Loading Optimization**: In `getAllConversations()` method, use 
`IN` query to load all conversation messages at once, avoiding N+1 query problem
   4. **Transaction Management**: Add `@Transactional` annotation to 
`deleteConversation()` method to ensure atomicity of delete operations
   
   ## Testing
   
   
https://github.com/user-attachments/assets/b9f3f29b-2746-481e-a29e-aef20605e81c
   
   
   
   ## Checklist
   
   - [x]  I have read the [Contributing 
Guide](https://hertzbeat.apache.org/docs/community/code_style_and_quality_guide)
   - [ ]  I have written the necessary doc or comment.
   - [ ]  I have added the necessary unit tests and all cases have passed.
   
   ## Add or update API
   
   - [ ] I have added the necessary [e2e 
tests](https://github.com/apache/hertzbeat/tree/master/e2e) and all cases have 
passed.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to