fix(document): add trainingLabels to Document.full entity graph #642

Closed
opened 2026-05-20 08:13:49 +02:00 by marcel · 0 comments
Owner

Problem

GET /api/documents/{id} returns HTTP 500 since trainingLabels was made LAZY in #467.

Root cause: Document.trainingLabels is @ElementCollection(fetch = FetchType.LAZY) but is not included in the Document.full entity graph:

@NamedEntityGraph(name = "Document.full", attributeNodes = {
        @NamedAttributeNode("sender"),
        @NamedAttributeNode("receivers"),
        @NamedAttributeNode("tags")
        // trainingLabels missing!
})

DocumentRepository.findById() uses @EntityGraph("Document.full"), which loads sender/receivers/tags eagerly. The transaction closes when getDocumentById() returns. When Jackson later serializes the Document, it accesses trainingLabels with no Hibernate session open → LazyInitializationException.

Error in staging logs:

org.hibernate.LazyInitializationException: Cannot lazily initialize collection of role
'org.raddatz.familienarchiv.document.Document.trainingLabels' with key
'52beeb09-ffd1-4cd3-b028-db55b4d280b1' (no session)
  through reference chain: ...Document["trainingLabels"]

Fix

Add trainingLabels to Document.full:

@NamedEntityGraph(name = "Document.full", attributeNodes = {
        @NamedAttributeNode("sender"),
        @NamedAttributeNode("receivers"),
        @NamedAttributeNode("tags"),
        @NamedAttributeNode("trainingLabels")
})

Acceptance criteria

  • GET /api/documents/{id} returns 200 with a document that has trainingLabels
  • Existing lazy-loading tests in DocumentLazyLoadingTest pass
  • No N+1 regression (entity graph JOIN already covers it)
## Problem `GET /api/documents/{id}` returns HTTP 500 since `trainingLabels` was made LAZY in #467. Root cause: `Document.trainingLabels` is `@ElementCollection(fetch = FetchType.LAZY)` but is **not** included in the `Document.full` entity graph: ```java @NamedEntityGraph(name = "Document.full", attributeNodes = { @NamedAttributeNode("sender"), @NamedAttributeNode("receivers"), @NamedAttributeNode("tags") // trainingLabels missing! }) ``` `DocumentRepository.findById()` uses `@EntityGraph("Document.full")`, which loads sender/receivers/tags eagerly. The transaction closes when `getDocumentById()` returns. When Jackson later serializes the `Document`, it accesses `trainingLabels` with no Hibernate session open → `LazyInitializationException`. Error in staging logs: ``` org.hibernate.LazyInitializationException: Cannot lazily initialize collection of role 'org.raddatz.familienarchiv.document.Document.trainingLabels' with key '52beeb09-ffd1-4cd3-b028-db55b4d280b1' (no session) through reference chain: ...Document["trainingLabels"] ``` ## Fix Add `trainingLabels` to `Document.full`: ```java @NamedEntityGraph(name = "Document.full", attributeNodes = { @NamedAttributeNode("sender"), @NamedAttributeNode("receivers"), @NamedAttributeNode("tags"), @NamedAttributeNode("trainingLabels") }) ``` ## Acceptance criteria - `GET /api/documents/{id}` returns 200 with a document that has `trainingLabels` - Existing lazy-loading tests in `DocumentLazyLoadingTest` pass - No N+1 regression (entity graph JOIN already covers it)
marcel added the P1-highbug labels 2026-05-20 08:13:54 +02:00
Sign in to join this conversation.
No Label P1-high bug
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: marcel/familienarchiv#642