Skip to main content

Adopt Jackson Hibernate Module to Serialize Lazy Proxies

Context

  • JSON serialization of JPA entities failed when encountering Hibernate lazy proxies (ByteBuddy interceptors), e.g., Application.openAPISpec.
  • Stack traces showed InvalidDefinitionException with hibernateLazyInitializer leaking into the JSON path.

Decision

  • Add com.fasterxml.jackson.datatype:jackson-datatype-hibernate5 and register Hibernate5Module in the central ObjectMapper.
  • Disable forced lazy loading and enable identifier serialization for uninitialized proxies.
  • Add Jackson mixins to ignore proxy internals (hibernateLazyInitializer, handler).

Consequences

  • Controllers can safely return entities with optional lazy associations without triggering serialization errors.
  • If full graph serialization is desired, opt-in with fetch-join/DTO mapping per endpoint.
  • The change is backward compatible and internal to serialization; no API contract changes.

References

  • valkyrai/pom.xml
  • valkyrai/src/main/java/com/valkyrlabs/valkyrai/config/JacksonConfig.java