{"record":{"id":"0fc6890f3531336f","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type-0fc689","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/Jackson3XmlFormatMapper.java","lineNumber":233,"sourceCode":"\t\t\t\t\tcollectionWrapper = new CollectionWrapper<>( list );\n\t\t\t\t}\n\t\t\t\treturn writeValueAsString(\n\t\t\t\t\t\tcollectionWrapper,\n\t\t\t\t\t\tjavaType,\n\t\t\t\t\t\tnew ParameterizedTypeImpl( CollectionWrapper.class,\n\t\t\t\t\t\t\t\tnew Type[] {javaType.getJavaTypeClass().getComponentType()}, null )\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn writeValueAsString( value, javaType, javaType.getJavaType() );\n\t}\n\n\tprivate <T> String writeValueAsString(Object value, JavaType<T> javaType, Type type) {\n\t\ttry {\n\t\t\treturn xmlMapper.writerFor( xmlMapper.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: \" + javaType, e );\n\t\t}\n\t}\n\n\t@JsonRootName(value = \"Collection\")\n\tpublic static class CollectionWrapper<E> {\n\t\t@JacksonXmlElementWrapper(useWrapping = false)\n\t\t@JacksonXmlProperty(localName = \"e\")\n\t\tCollection<E> value;\n\n\t\tpublic CollectionWrapper() {\n\t\t\tthis.value = new ArrayList<>();\n\t\t}\n\n\t\tpublic CollectionWrapper(Collection<E> value) {\n\t\t\tthis.value = value;\n\t\t}\n\t}\n","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3XmlFormatMapper.java#L215-L251","documentation":"Serialization half of the Jackson 3 XML FormatMapper: writeValueAsString() renders the mapped Java value to XML via xmlMapper.writerFor(type) and wraps any JacksonException as IllegalArgumentException('Could not serialize object of java type: <type>'). The value could not be written as XML at flush time.","triggerScenarios":"Persisting an @JdbcTypeCode(SqlTypes.XML) attribute whose value has no XML serializer (missing getters, exotic field type without module), contains a map key or property name that is not a valid XML element name, or builds a cyclic graph the writer cannot resolve.","commonSituations":"Map<String, ...> fields persisted to XML where keys contain spaces or start with digits; DTO fields renamed without @JacksonXmlProperty; Jackson 2 → Jackson 3 migration changing module registration; lazy proxies inside the XML value.","solutions":["Read the cause JacksonException — it names the property/key that failed","Sanitize element-name sources: map keys and @JacksonXmlProperty localNames must be valid XML names (letters/underscores, no spaces)","Expose getters or use records so the mapper can see the properties; break cycles with @JsonIgnore","Inject a configured XmlMapper via new Jackson3XmlFormatMapper(xmlMapper, legacyFormat) and hibernate.type.xml_format_mapper"],"exampleFix":"// before: map keys become XML element names\n@JdbcTypeCode(SqlTypes.XML)\nprivate Map<String, String> translations; // key \"en-US\" -> invalid element name\n// after: wrap map entries so keys become attribute values\npublic static class Entry {\n    @JacksonXmlProperty(isAttribute = true)\n    public String lang;\n    @JacksonXmlText\n    public String text;\n}\nprivate List<Entry> translations;","handlingStrategy":"try-catch","validationCode":"// ensure every map key / property name destined for XML is a valid element name\nstatic boolean isValidXmlElementName(String name) {\n    return name != null && !name.isEmpty()\n            && (Character.isLetter(name.charAt(0)) || name.charAt(0) == '_')\n            && name.chars().allMatch(c -> Character.isLetterOrDigit(c) || c == '_' || c == '-' || c == '.');\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 property/key that cannot be written\n        throw new MappingConfigurationException(\"Value not XML-serializable: \" + cause.getMessage(), ex);\n    }\n    throw ex;\n}","preventionTips":["Do not let raw map keys become XML element names - wrap entries as shown in exampleFix","Round-trip test XML-mapped entities in CI","Supply a configured XmlMapper via new Jackson3XmlFormatMapper(xmlMapper, legacyFormat) + hibernate.type.xml_format_mapper when defaults are not enough"],"tags":["hibernate","xml","jackson","serialization","orm-mapping"],"backgroundTag":"xml-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}