{"record":{"id":"99deba143605ef5a","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type","errorCode":null,"errorMessage":"Could not serialize object of java type: {}","messagePattern":"Could not serialize object of java type: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3JsonFormatMapper.java","lineNumber":89,"sourceCode":"\t}\n\n\t@Override\n\tpublic <T> T fromString(CharSequence charSequence, Type type) {\n\t\ttry {\n\t\t\treturn jsonMapper.readValue( charSequence.toString(), jsonMapper.constructType( type ) );\n\t\t}\n\t\tcatch (JacksonException e) {\n\t\t\tthrow new IllegalArgumentException( \"Could not deserialize string to java type: \" + type, e );\n\t\t}\n\t}\n\n\t@Override\n\tpublic <T> String toString(T value, Type type) {\n\t\ttry {\n\t\t\treturn jsonMapper.writerFor( jsonMapper.constructType( type ) ).writeValueAsString( value );\n\t\t}\n\t\tcatch (JacksonException e) {\n\t\t\tthrow new IllegalArgumentException( \"Could not serialize object of java type: \" + type, e );\n\t\t}\n\t}\n}\n","sourceCodeStart":71,"sourceCodeEnd":93,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3JsonFormatMapper.java#L71-L93","documentation":"Serialization half of the Jackson 3 JSON FormatMapper: toString() calls jsonMapper.writerFor(type).writeValueAsString(value) and wraps any JacksonException as IllegalArgumentException('Could not serialize object of java type: <type>') with the cause attached. The mapped Java object could not be rendered to JSON — typically at flush time of a JSON-mapped attribute.","triggerScenarios":"Persisting/updating a @JdbcTypeCode(SqlTypes.JSON) attribute whose value Jackson cannot serialize: no serializer for a field type (missing getters, exotic type without module), self-referencing object graph (StackOverflowError-adjacent JsonMappingException), or an uninitialized lazy proxy inside the value.","commonSituations":"Adding a field of a type the mapper does not handle (no module registered); Jackson 2 → Jackson 3 migration changing the registered module set; lazy-loaded associations embedded in a JSON value detached from the session; cyclic object graphs returned by builders.","solutions":["Read the cause JacksonException — it names the offending property and reason","Fix the DTO: expose getters (or use records), break cycles with @JsonIgnore/@JsonManagedReference+@JsonBackReference, initialize or detach lazy proxies before assignment","Inject a preconfigured JsonMapper via new Jackson3JsonFormatMapper(mapper) and hibernate.type.json_format_mapper so needed modules/serializers are registered","As a stopgap, convert the value to a plain serializable DTO before storing it"],"exampleFix":"// before: cyclic parent/child stored as JSON\n// -> 'Could not serialize object of java type: Parent' (JsonMappingException: cycle)\nentity.setTree(parentNodeWithChildrenPointingBack);\n// after: break the cycle\npublic class Node {\n    public String name;\n    @JsonIgnore\n    public Node parent;          // not serialized\n    public List<Node> children;\n}","handlingStrategy":"try-catch","validationCode":"// verify serializability before assigning/flushing\nstatic boolean isSerializable(Object value, tools.jackson.databind.json.JsonMapper mapper) {\n    try {\n        mapper.writeValueAsString(value);\n        return true;\n    } catch (tools.jackson.core.JacksonException e) {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.merge(entity);\n    session.flush();\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage() != null && ex.getMessage().startsWith(\"Could not serialize object of java type\")) {\n        Throwable cause = ex.getCause(); // names the offending property\n        throw new MappingConfigurationException(\"Value not JSON-serializable: \" + cause.getMessage(), ex);\n    }\n    throw ex;\n}","preventionTips":["Round-trip test every JSON-mapped entity (serialize then deserialize) in CI","Break object cycles and initialize/detach lazy proxies before storing values in JSON fields","Register all needed Jackson modules on a custom mapper passed via hibernate.type.json_format_mapper"],"tags":["hibernate","json","jackson","serialization","orm-mapping"],"backgroundTag":"json-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}