{"record":{"id":"473f6718ab576c64","repo":"hibernate/hibernate-orm","slug":"could-not-deserialize-string-to-java-type-473f67","errorCode":null,"errorMessage":"Could not deserialize string to java type: {}","messagePattern":"Could not deserialize string to java type: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3XmlFormatMapper.java","lineNumber":161,"sourceCode":"\t\t\t\t\t);\n\t\t\t\t\treturn javaType.wrap( collectionWrapper.value, wrapperOptions );\n\t\t\t\t}\n\t\t\t\telse if ( javaType.getJavaTypeClass().isArray() ) {\n\t\t\t\t\tfinal CollectionWrapper<?> collectionWrapper = xmlMapper.readValue(\n\t\t\t\t\t\t\tcharSequence.toString(),\n\t\t\t\t\t\t\txmlMapper.constructType( new ParameterizedTypeImpl( CollectionWrapper.class,\n\t\t\t\t\t\t\t\t\tnew Type[] {javaType.getJavaTypeClass().getComponentType()}, null ) )\n\t\t\t\t\t);\n\t\t\t\t\treturn javaType.wrap( collectionWrapper.value, wrapperOptions );\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn xmlMapper.readValue(\n\t\t\t\t\tcharSequence.toString(),\n\t\t\t\t\txmlMapper.constructType( javaType.getJavaType() )\n\t\t\t);\n\t\t}\n\t\tcatch (JacksonException e) {\n\t\t\tthrow new IllegalArgumentException( \"Could not deserialize string to java type: \" + javaType, e );\n\t\t}\n\t}\n\n\t@Override\n\tpublic <T> String toString(T value, JavaType<T> javaType, WrapperOptions wrapperOptions) {\n\t\tif ( javaType.getJavaType() == String.class || javaType.getJavaType() == Object.class ) {\n\t\t\treturn (String) value;\n\t\t}\n\t\tif ( !legacyFormat ) {\n\t\t\tif ( Map.class.isAssignableFrom( javaType.getJavaTypeClass() ) ) {\n\t\t\t\tfinal Type keyType;\n\t\t\t\tfinal Type elementType;\n\t\t\t\tif ( javaType.getJavaType() instanceof ParameterizedType parameterizedType ) {\n\t\t\t\t\tkeyType = parameterizedType.getActualTypeArguments()[0];\n\t\t\t\t\telementType = parameterizedType.getActualTypeArguments()[1];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tkeyType = Object.class;","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3XmlFormatMapper.java#L143-L179","documentation":"XML counterpart of the Jackson 3 mapper, used for @JdbcTypeCode(SqlTypes.XML) attributes: fromString() binds the XML column content with an XmlMapper and wraps any JacksonException as IllegalArgumentException('Could not deserialize string to java type: <type>'). The stored XML does not bind to the mapped Java type — wrong root/element names, collection wrapping mismatch (non-legacy format expects a 'Collection' root with 'e' elements), or value type mismatch.","triggerScenarios":"Loading an @JdbcTypeCode(SqlTypes.XML) entity when: the root element name does not match the mapping; collections are stored in the other encoding style (the mapper was built with the hibernate.type.xml_format_mapper.legacy_format flag mismatching the data); the XML comes from an external producer with different element names; a field type changed (single element vs List).","commonSituations":"Migrating Hibernate versions where the default XML collection encoding changed (legacy wrapped vs unwrapped format); consuming external XML not generated by Hibernate; changing the mapped Java type without transforming existing rows.","solutions":["Compare the stored XML with what Hibernate writes for the same mapping (root element, 'e' items for collections)","Set hibernate.type.xml_format_mapper.legacy_format=true (or false) to match how the existing data was encoded","Adjust mapping annotations (@JsonRootName, @JacksonXmlProperty/@JacksonXmlElementWrapper) or transform the stored XML to the expected shape","Read the cause JacksonException for the exact element/path that failed"],"exampleFix":"# before: rows written by an older Hibernate (wrapped collection format), new mapper reads them\nhibernate.type.xml_format_mapper.legacy_format=false\n# after: match the reader to the stored format\nhibernate.type.xml_format_mapper.legacy_format=true","handlingStrategy":"try-catch","validationCode":"// check the stored XML binds before mass-loading\nstatic <T> void validateXmlMatches(String xml, tools.jackson.databind.JavaType type,\n                                   tools.jackson.dataformat.xml.XmlMapper mapper) {\n    try {\n        mapper.readValue(xml, type);\n    } catch (tools.jackson.core.JacksonException e) {\n        throw new IllegalArgumentException(\"XML column incompatible: \" + e.getMessage(), e);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    MyEntity e = session.find(MyEntity.class, id);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage() != null && ex.getMessage().startsWith(\"Could not deserialize string to java type\")) {\n        Throwable cause = ex.getCause(); // JacksonException naming element/path\n        throw new DataQualityException(\"XML column incompatible with mapping: \" + cause.getMessage(), ex);\n    }\n    throw ex;\n}","preventionTips":["After Hibernate upgrades, verify hibernate.type.xml_format_mapper.legacy_format matches how existing rows were written","Keep @JsonRootName / @JacksonXmlProperty annotations in sync with stored element names","Migrate stored XML deliberately when changing collection mappings (List vs single element)"],"tags":["hibernate","xml","jackson","deserialization","orm-mapping"],"backgroundTag":"xml-deserialization-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}