{"record":{"id":"99f30bc974b6e808","repo":"hibernate/hibernate-orm","slug":"xml-not-properly-formatted","errorCode":null,"errorMessage":"XML not properly formatted: ","messagePattern":"XML not properly formatted: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/XmlHelper.java","lineNumber":287,"sourceCode":"\t}\n\n\tpublic static <X> X fromString(\n\t\t\tEmbeddableMappingType embeddableMappingType,\n\t\t\tString string,\n\t\t\tboolean returnEmbeddable,\n\t\t\tWrapperOptions options) throws SQLException {\n\t\tif ( NULL_TAG.equals( string ) ) {\n\t\t\treturn null;\n\t\t}\n\t\tint contentEnd = string.length() - 1;\n\t\twhile ( contentEnd >= 0 ) {\n\t\t\tif ( !Character.isWhitespace( string.charAt( contentEnd ) ) ) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcontentEnd--;\n\t\t}\n\t\tif ( !string.startsWith( START_TAG ) || !string.regionMatches( contentEnd - END_TAG.length()+ 1, END_TAG, 0, END_TAG.length() ) ) {\n\t\t\tthrow new IllegalArgumentException( \"XML not properly formatted: \" + string );\n\t\t}\n\t\tint end;\n\t\tfinal Object[] array;\n\t\tif ( embeddableMappingType == null ) {\n\t\t\tassert !returnEmbeddable;\n\t\t\tfinal List<Object> values = new ArrayList<>( 8 );\n\t\t\tend = fromString( string, values, START_TAG.length() );\n\t\t\tarray = values.toArray();\n\t\t}\n\t\telse {\n\t\t\tarray = new Object[embeddableMappingType.getJdbcValueCount() + ( embeddableMappingType.isPolymorphic() ? 1 : 0 )];\n\t\t\tend = fromString( embeddableMappingType, string, returnEmbeddable, options, array, START_TAG.length() );\n\t\t}\n\t\tassert end + END_TAG.length() == contentEnd + 1;\n\n\t\tif ( returnEmbeddable ) {\n\t\t\tfinal StructAttributeValues attributeValues = StructHelper.getAttributeValues( embeddableMappingType, array, options );\n\t\t\t//noinspection unchecked","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/XmlHelper.java#L269-L305","documentation":"XmlHelper.fromString expects an XML aggregate column to hold Hibernate's exact format: a root element <e> ... </e> (or <e/> for null). If the string does not start with <e> and end with </e>, reading the aggregate fails with this IllegalArgumentException. The format is Hibernate-internal, not arbitrary XML.","triggerScenarios":"Loading an entity with an XML-mapped embeddable when the column content lacks the <e>...</e> wrapper: arbitrary XML written by another tool, an empty or padded string, or content in a different aggregate format (e.g. a JSON aggregate value left in the column).","commonSituations":"Column pre-populated by external writers; switching a column between JSON and XML aggregate mappings; data exported from another ORM; leftover test data.","solutions":["Rewrite column data into the <e>...</e> aggregate format (or null it) with a migration script.","Write values through Hibernate once so the column converges on the correct format.","Verify the mapping declares the intended aggregate type (@JdbcTypeCode(SqlTypes.SQLXML) or the dialect XML type) matching the column."],"exampleFix":"-- PostgreSQL: wrap stray content into Hibernate's root tag\nUPDATE t SET xml_col = '<e>' || xml_col || '</e>'\nWHERE xml_col IS NOT NULL AND xml_col <> '<e/>'\n  AND NOT (xml_col LIKE '<e>%' AND xml_col LIKE '%</e>');","handlingStrategy":"validation","validationCode":"static boolean isHibernateAggregateXml(String s) {\n    return s == null || \"<e/>\".equals(s)\n        || (s.startsWith(\"<e>\") && s.endsWith(\"</e>\") && s.length() >= 7);\n}\n// audit rows: SELECT id FROM t WHERE NOT (xml_col IS NULL OR xml_col = '<e/>'\n//   OR (xml_col LIKE '<e>%' AND xml_col LIKE '%</e>'));","typeGuard":null,"tryCatchPattern":"try {\n    return session.find(Person.class, id);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage() != null && ex.getMessage().startsWith(\"XML not properly formatted\")) {\n        quarantine(id, ex.getMessage());\n        return null;\n    }\n    throw ex;\n}","preventionTips":["Validate column format with a SQL audit before switching a table to XML aggregate mappings.","Never let external tools write raw XML into aggregate columns.","Use NULL (or '<e/>') for empty values instead of empty strings."],"tags":["hibernate","xml","xml-aggregate","data-format"],"backgroundTag":"malformed-xml-data","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}