{"record":{"id":"82f756b8b9803878","repo":"hibernate/hibernate-orm","slug":"error-serializing-entity","errorCode":null,"errorMessage":"Error serializing entity","messagePattern":"Error serializing entity","errorType":"exception","errorClass":"UncheckedIOException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/DescriptiveJsonGeneratingVisitor.java","lineNumber":59,"sourceCode":"\n\tprivate Map<String, IdentitySet<Object>> circularityTracker;\n\n\t@Override\n\tprotected void serializeEntity(Object value, EntityMappingType entityType, WrapperOptions options, JsonDocumentWriter writer) {\n\t\tfinal EntityIdentifierMapping identifierMapping = entityType.getIdentifierMapping();\n\t\ttrackingEntity( value, entityType, shouldProcessEntity -> {\n\t\t\ttry {\n\t\t\t\twriter.startObject();\n\t\t\t\twriter.objectKey( identifierMapping.getAttributeName() );\n\t\t\t\tserializeEntityIdentifier( value, identifierMapping, options, writer );\n\t\t\t\tif ( shouldProcessEntity ) {\n\t\t\t\t\t// if it wasn't already encountered, append all properties\n\t\t\t\t\tserializeObjectValues( entityType, value, options, writer );\n\t\t\t\t}\n\t\t\t\twriter.endObject();\n\t\t\t}\n\t\t\tcatch (IOException e) {\n\t\t\t\tthrow new UncheckedIOException( \"Error serializing entity\", e );\n\t\t\t}\n\t\t} );\n\t}\n\n\tprivate void trackingEntity(Object entity, EntityMappingType entityType, Consumer<Boolean> action) {\n\t\tif ( circularityTracker == null ) {\n\t\t\tcircularityTracker = new HashMap<>();\n\t\t}\n\t\tfinal IdentitySet<Object> entities = circularityTracker.computeIfAbsent(\n\t\t\t\tentityType.getEntityName(),\n\t\t\t\tk -> new IdentitySet<>()\n\t\t);\n\t\tfinal boolean added = entities.add( entity );\n\t\taction.accept( added );\n\t\tif ( added ) {\n\t\t\tentities.remove( entity );\n\t\t}\n\t}","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/DescriptiveJsonGeneratingVisitor.java#L41-L77","documentation":"DescriptiveJsonGeneratingVisitor, used by Hibernate Assistant tooling to render query results as descriptive JSON, wraps any IOException from the underlying JsonDocumentWriter in an UncheckedIOException while serializing an entity (identifier plus properties). It signals that the writer stream failed mid-entity, not that the entity mapping is invalid.","triggerScenarios":"Serializing a query result entity to descriptive JSON when the writer's target stream throws: a closed writer, an IO error on the underlying sink, or a custom JsonDocumentWriter implementation failing inside startObject/objectKey/endObject calls.","commonSituations":"Tooling integrations (Hibernate Assistant result export) writing to streams with lifecycle bugs; very large object graphs exhausting buffers; interleaved close() calls during result rendering.","solutions":["Check the writer/stream lifecycle: do not close the sink before serialization completes.","Catch UncheckedIOException in the tooling integration and retry with a fresh writer.","Reduce the serialized graph (projection instead of full entities) if size triggers the failure."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    return resultsJsonSerializer.toJson(resultList);\n} catch (UncheckedIOException ex) {\n    if (\"Error serializing entity\".equals(ex.getMessage())) {\n        // writer/stream failed mid-entity: report partial failure, retry with fresh writer\n        return retryWithFreshWriter(resultList, ex);\n    }\n    throw ex;\n}","preventionTips":["Keep the JsonDocumentWriter open for the entire serialization call.","Do not share writers across threads or close the sink early in tooling pipelines.","Serialize projections for very large graphs instead of full entities."],"tags":["hibernate","json","serialization","tooling","io"],"backgroundTag":"json-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}