{"record":{"id":"4155e31727a4e36e","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type-4155e3","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/jakartajson/JsonBJsonFormatMapper.java","lineNumber":50,"sourceCode":"\t}\n\n\t@Override\n\tpublic <T> T fromString(CharSequence charSequence, Type type) {\n\t\ttry {\n\t\t\treturn jsonb.fromJson( charSequence.toString(), type );\n\t\t}\n\t\tcatch (JsonbException 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 jsonb.toJson( value, type );\n\t\t}\n\t\tcatch (JsonbException e) {\n\t\t\tthrow new IllegalArgumentException( \"Could not serialize object of java type: \" + type, e );\n\t\t}\n\t}\n}\n","sourceCodeStart":32,"sourceCodeEnd":54,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jakartajson/JsonBJsonFormatMapper.java#L32-L54","documentation":"JsonBJsonFormatMapper.toString serializes a SqlTypes.JSON attribute value to column text using jakarta.json.bind Jsonb.toJson (JsonBJsonFormatMapper.java:44-51). This IllegalArgumentException wraps a JsonbException thrown while serializing the current value, i.e. JSON-B cannot render the attribute's runtime object. The Yasson message in the cause names the property or type it failed on.","triggerScenarios":"Flushing an entity whose JSON attribute holds a value JSON-B cannot serialize (e.g. java.time types without a format JSON-B supports, polymorphic types without @JsonbTypeInfo, custom classes without readable properties); getters of the value throwing exceptions during serialization.","commonSituations":"Moving an application from a Jackson-based mapper to JSON-B where serialization defaults differ; adding java.time or generic Object fields to a JSON-mapped DTO; custom Jsonb instances configured with strict property visibility.","solutions":["Read the cause message to find the offending property/type.","Annotate the value type for JSON-B (@JsonbProperty, @JsonbDateFormat, @JsonbTransient) or convert java.time fields to strings.","Configure a Jsonb with the needed adapters/serializers and pass it via new JsonBJsonFormatMapper(jsonb) through the hibernate.type.json_format_mapper setting.","Keep the attribute type a plain DTO graph so any JSON-B runtime can serialize it."],"exampleFix":"// before - OffsetDateTime field fails under default Yasson serialization\npublic class Audit {\n    public OffsetDateTime changedAt;\n}\n\n// after - explicit format makes it serializable\npublic class Audit {\n    @JsonbDateFormat(\"yyyy-MM-dd'T'HH:mm:ssXXX\")\n    public OffsetDateTime changedAt;\n}","handlingStrategy":"validation","validationCode":"// Confirm JSON-B can serialize a value before depending on the flush\ntry ( Jsonb jsonb = JsonbBuilder.create() ) {\n    jsonb.toJson( value ); // throws JsonbException early if not serializable\n}","typeGuard":null,"tryCatchPattern":"try {\n    tx.commit();\n} catch ( IllegalArgumentException e ) {\n    if ( e.getCause() instanceof jakarta.json.bind.JsonbException je ) {\n        // fix the value type (annotations/adapters) per je.getMessage()\n    } else throw e;\n}","preventionTips":["Annotate java.time fields with @JsonbDateFormat; add JsonbAdapter for exotic types.","Keep the attribute type to plain DTOs; avoid polymorphic fields unless annotated for JSON-B.","Prefer configuring a custom Jsonb instance via the mapper setting over fighting defaults."],"tags":["hibernate","json","json-b","yasson","orm","serialization"],"backgroundTag":"json-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}