{"record":{"id":"5c4931571e953cd5","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type-5c4931","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/JacksonXmlFormatMapper.java","lineNumber":235,"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 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: \" + javaType, e );\n\t\t}\n\t}\n\n\t@JacksonXmlRootElement(localName = \"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":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jackson/JacksonXmlFormatMapper.java#L217-L253","documentation":"JacksonXmlFormatMapper.toString serializes a SqlTypes.SQLXML attribute value to XML via the Jackson XmlMapper (JacksonXmlFormatMapper.java:235-240). This IllegalArgumentException wraps a JsonProcessingException thrown while writerFor(type).writeValueAsString(value) runs, meaning the value cannot be rendered as XML by Jackson-dataformat-xml. The cause carries the precise reason (no serializer, non-String map keys, self-reference).","triggerScenarios":"Flushing an entity whose XML attribute value contains a Map with non-String keys (a known Jackson-XML limitation), a directly self-referencing object, java.time values without the corresponding module, or a root type Jackson-XML cannot serialize (missing properties/annotations for unwrapped collections).","commonSituations":"Switching a JSON attribute to @JdbcTypeCode(SqlTypes.SQLXML) and hitting XML-specific constraints that JSON tolerated (map keys, arrays without wrapper elements); adding java.time or polymorphic fields to an existing XML-mapped DTO; custom XmlMapper built without findModules.","solutions":["Read the cause message to find the offending field of the value being flushed.","Use String keys in Map attributes (or a List of key/value entries annotated with @JacksonXmlElementWrapper/@JacksonXmlProperty).","Annotate the type with Jackson-XML annotations (@JacksonXmlRootElement, @JacksonXmlProperty) so the value renders as valid XML.","Register required Jackson modules on the XmlMapper you pass via JacksonXmlFormatMapper(ObjectMapper, legacyFormat).","Keep the XML attribute type simple (DTO graph without cycles)."],"exampleFix":"// before - non-String map keys cannot be serialized to XML\n@JdbcTypeCode(SqlTypes.SQLXML)\nMap<UUID, String> items; // IllegalArgumentException on flush\n\n// after - String keys render fine\n@JdbcTypeCode(SqlTypes.SQLXML)\nMap<String, String> items;","handlingStrategy":"validation","validationCode":"// Verify the attribute value renders as XML before it reaches Hibernate\nstatic void assertXmlSerializable(Object value) {\n    try {\n        XmlMapper xml = XmlMapper.builder().findAndAddModules().build();\n        xml.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    tx.commit();\n} catch ( IllegalArgumentException e ) {\n    if ( e.getCause() instanceof com.fasterxml.jackson.core.JsonProcessingException jpe ) {\n        // e.g. non-String map keys - fix the attribute structure\n    } else throw e;\n}","preventionTips":["Avoid Map with non-String keys in SQLXML attributes - use List<Entry> or String keys.","Unit-test XML serialization of each mapped type with a real XmlMapper.","Keep XML attributes free of cycles and unannotated polymorphism."],"tags":["hibernate","xml","jackson","sqlxml","orm","serialization"],"backgroundTag":"xml-serialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}