{"record":{"id":"350bc02217e4ea0d","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type-350bc0","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/JacksonJsonFormatMapper.java","lineNumber":86,"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","sourceCodeStart":68,"sourceCodeEnd":90,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jackson/JacksonJsonFormatMapper.java#L68-L90","documentation":"Serialization half of Hibernate 6's Jackson 2 JSON FormatMapper: toString() calls objectMapper.writerFor(type).writeValueAsString(value) and wraps JsonProcessingException as IllegalArgumentException('Could not serialize object of java type: <type>') with the cause attached. The Java value of a JSON-mapped attribute could not be rendered to JSON, typically at flush time.","triggerScenarios":"Persisting/updating a @JdbcTypeCode(SqlTypes.JSON) attribute whose value Jackson 2 cannot serialize: java.time types without JavaTimeModule ('Java 8 date/time type not supported by default'), no serializer for a private-fielded type (InvalidDefinitionException), cyclic object graph, or an uninitialized Hibernate proxy inside the value.","commonSituations":"Adding Instant/LocalDateTime/Duration fields to a JSON DTO without registering jackson-datatype-jsr310 on Hibernate's mapper; DTOs with only private fields and no getters; lazy associations serialized while detached; bidirectional references.","solutions":["Read the cause JsonProcessingException — it names the offending property and exact reason","Register the needed modules on a custom ObjectMapper and inject it via new JacksonJsonFormatMapper(mapper) + hibernate.type.json_format_mapper","Fix the DTO: add getters/records, break cycles (@JsonIgnore, @JsonManagedReference/@JsonBackReference), detach or initialize lazy proxies before storing","Add a round-trip integration test for every JSON-mapped entity so serializer gaps surface in CI"],"exampleFix":"// before: flush fails 'Could not serialize object of java type: Event'\n// cause: Java 8 date/time type `java.time.Instant` not supported by default\n@JdbcTypeCode(SqlTypes.JSON)\nprivate EventPayload payload; // contains Instant fields\n// after\nObjectMapper om = new ObjectMapper()\n        .registerModule(new JavaTimeModule())\n        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);\nproperties.put(AvailableSettings.JSON_FORMAT_MAPPER, () -> new JacksonJsonFormatMapper(om));","handlingStrategy":"try-catch","validationCode":"static boolean serializableWithJackson2(Object value, com.fasterxml.jackson.databind.ObjectMapper mapper) {\n    try {\n        mapper.writeValueAsString(value);\n        return true;\n    } catch (com.fasterxml.jackson.core.JsonProcessingException 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        com.fasterxml.jackson.databind.JsonMappingException cause =\n                (com.fasterxml.jackson.databind.JsonMappingException) ex.getCause();\n        // cause.getPath() lists the exact property chain that failed\n        throw new MappingConfigurationException(\"JSON value not serializable: \" + cause.getPathReference(), ex);\n    }\n    throw ex;\n}","preventionTips":["Register JavaTimeModule (jackson-datatype-jsr310) on the mapper Hibernate uses when storing java.time fields as JSON","Give DTOs visible getters or use records; break cycles with @JsonIgnore","Round-trip test every JSON-mapped entity before release"],"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"}