{"record":{"id":"93726d20823dcb79","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type-93726d","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/JacksonOsonFormatMapper.java","lineNumber":99,"sourceCode":"\t}\n\n\t@Override\n\tpublic <T> T fromString(CharSequence charSequence, Type type) {\n\t\ttry {\n\t\t\treturn objectMapper.readValue( charSequence.toString(), objectMapper.constructType( type ) );\n\t\t}\n\t\tcatch (JsonProcessingException 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 objectMapper.writerFor( objectMapper.constructType( type ) ).writeValueAsString( value );\n\t\t}\n\t\tcatch (JsonProcessingException e) {\n\t\t\tthrow new IllegalArgumentException( \"Could not serialize object of java type: \" + type, e );\n\t\t}\n\t}\n\n}\n","sourceCodeStart":81,"sourceCodeEnd":104,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jackson/JacksonOsonFormatMapper.java#L81-L104","documentation":"JacksonOsonFormatMapper.toString serializes a SqlTypes.JSON attribute value to column text using Jackson (JacksonOsonFormatMapper.java:94-101). This IllegalArgumentException wraps a JsonProcessingException raised while objectMapper.writerFor(type).writeValueAsString(value) runs, i.e. Jackson cannot serialize the current attribute value. The concrete Jackson reason (no serializer, direct self-reference, missing module for a java.time type) is in the cause.","triggerScenarios":"INSERT/UPDATE flush of an entity whose JSON attribute value contains a type Jackson cannot serialize (e.g. java.time.Instant when JavaTimeModule is not registered); a getter of the value throwing an exception; a directly self-referencing object (JsonMappingException: Direct self-reference leading to cycle); a bidirectional relation inside the JSON attribute without @JsonIgnore/@JsonBackReference.","commonSituations":"Adding java.time fields or polymorphic fields to a JSON-mapped POJO after the mapping worked; passing a custom ObjectMapper (new JacksonOsonFormatMapper(objectMapper)) that was built without findModules; value objects with only getters that throw for uninitialized state.","solutions":["Read the cause's message to identify the offending property/type of the value being flushed.","Annotate the JSON type for Jackson: @JsonIgnore / @JsonManagedReference+@JsonBackReference for cycles, @JsonSerialize adapters for exotic types.","If a custom ObjectMapper is used, register the needed modules (JavaTimeModule, etc.) before handing it to JacksonOsonFormatMapper.","Restructure the attribute to hold only Jackson-friendly types (strings, numbers, collections, plain DTOs)."],"exampleFix":"// before - Instant field but the mapper's ObjectMapper has no JavaTimeModule\npublic class Event {\n    public Instant occurredAt; // writeValueAsString fails on flush\n}\n\n// after - register the module on the mapper you pass in\nObjectMapper mapper = new ObjectMapper().findAndRegisterModules();\nsettings.put(AvailableSettings.JSON_FORMAT_MAPPER, new JacksonOsonFormatMapper(mapper));","handlingStrategy":"validation","validationCode":"// Verify the value serializes with a Jackson mapper before flushing\nstatic void assertSerializable(Object value) {\n    try {\n        new ObjectMapper().findAndRegisterModules().writeValueAsString( value );\n    } catch ( JsonProcessingException e ) {\n        throw new IllegalStateException( \"Value will fail on flush: \" + e.getOriginalMessage(), e );\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.persist( doc );\n    tx.commit();\n} catch ( IllegalArgumentException e ) {\n    if ( e.getCause() instanceof com.fasterxml.jackson.core.JsonProcessingException jpe ) {\n        // identify the offending field from jpe.getOriginalMessage() and fix the value/type\n    } else throw e;\n}","preventionTips":["Keep JSON attributes as plain DTO graphs without cycles, or annotate cycles with @JsonManagedReference/@JsonBackReference.","Register modules (JavaTimeModule etc.) on any custom ObjectMapper you hand to JacksonOsonFormatMapper via findAndRegisterModules.","Unit-test serialization of every type used in a JSON attribute.","Run assertSerializable-style checks in dev/test builds before commit/flush."],"tags":["hibernate","json","jackson","oracle","oson","orm","serialization"],"backgroundTag":"json-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}