{"record":{"id":"7bb93515c9ee46ee","repo":"hibernate/hibernate-orm","slug":"could-not-serialize-object-of-java-type-7bb935","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/jaxb/JaxbXmlFormatMapper.java","lineNumber":496,"sourceCode":"\t\t\t\t\t\t\tappender,\n\t\t\t\t\t\t\t( (BasicPluralJavaType<?>) javaType ).getElementJavaType(),\n\t\t\t\t\t\t\tcollectionElementTagName\n\t\t\t\t\t);\n\t\t\t\t\tfor ( int i = 0; i < length; i++ ) {\n\t\t\t\t\t\tlist.add( transformer.toJAXBElement( Array.get( value, i ) ) );\n\t\t\t\t\t}\n\t\t\t\t\tcollectionWrapper = new CollectionWrapper( list );\n\t\t\t\t}\n\t\t\t\tcreateMarshaller( context ).marshal( collectionWrapper, stringWriter );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfinal JAXBContext context = JAXBContext.newInstance( javaType.getJavaTypeClass() );\n\t\t\t\tcreateMarshaller( context ).marshal( value, stringWriter );\n\t\t\t}\n\t\t\treturn stringWriter.toString();\n\t\t}\n\t\tcatch (JAXBException e) {\n\t\t\tthrow new IllegalArgumentException( \"Could not serialize object of java type: \" + javaType, e );\n\t\t}\n\t}\n\n\t@Override\n\tpublic <T> void writeToTarget(T value, JavaType<T> javaType, Object target, WrapperOptions options) {\n\t}\n\n\t@Override\n\tpublic <T> T readFromSource(JavaType<T> javaType, Object source, WrapperOptions options) {\n\t\treturn null;\n\t}\n\n\tprivate JAXBElementTransformer createTransformer(\n\t\t\tStringBuilderSqlAppender appender,\n\t\t\tClass<?> elementClass,\n\t\t\tString tagName,\n\t\t\tObject exampleElement,\n\t\t\tJAXBIntrospector introspector,","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/jaxb/JaxbXmlFormatMapper.java#L478-L514","documentation":"JaxbXmlFormatMapper.toString marshals a SqlTypes.SQLXML attribute value to XML using a JAXB Marshaller (JaxbXmlFormatMapper.java:491-499). This IllegalArgumentException wraps a JAXBException thrown while marshalling, meaning JAXB cannot render the current value: missing @XmlRootElement on the class, a property JAXB cannot handle (Map with non-String keys, java.time without adapter), or a class not registered with the JAXBContext.","triggerScenarios":"Flushing an entity whose XML attribute class lacks @XmlRootElement; value graphs containing Map with complex keys, unadapted java.time fields, or objects without JAXB mappings; getters that throw during marshalling.","commonSituations":"Switching the XML mapper implementation under an existing mapping without changing annotations; adding Map or Optional fields to an XML-mapped DTO; refactoring the class so its no-arg constructor or annotations disappear.","solutions":["Add @XmlRootElement and @XmlAccessorType(XmlAccessType.FIELD) to the attribute type.","Replace unsupported structures: Map with non-String keys becomes a List of entry elements; java.time fields get @XmlJavaTypeAdapter adapters.","Verify a public no-arg constructor exists on every type in the value graph.","If the type is built for Jackson, switch the mapper with hibernate.type.xml_format_mapper=jackson-xml instead of JAXB-annotating it."],"exampleFix":"// before - class lacks @XmlRootElement, marshalling fails\npublic class Note { public String text; }\n\n// after - JAXB can marshal it as the document root\n@XmlRootElement(name = \"note\")\n@XmlAccessorType(XmlAccessType.FIELD)\npublic class Note { @XmlElement public String text; }","handlingStrategy":"try-catch","validationCode":"// Confirm a value marshals before it is flushed\nstatic void assertMarshallable(Object value) {\n    try {\n        JAXBContext.newInstance( value.getClass() )\n                .createMarshaller().marshal( value, new StringWriter() );\n    } catch ( JAXBException e ) {\n        throw new IllegalStateException( \"Value will fail on flush: \" + e.getMessage(), e );\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    tx.commit();\n} catch ( IllegalArgumentException e ) {\n    if ( e.getCause() instanceof jakarta.xml.bind.JAXBException jaxb ) {\n        // usually missing @XmlRootElement or an unhandled property type\n    } else throw e;\n}","preventionTips":["Never put Map with non-String keys or unadapted java.time inside a JAXB-mapped attribute.","Keep a marshalling unit test per SQLXML type to catch annotation drift.","Avoid mixing Jackson and JAXB annotations on the same mapped class."],"tags":["hibernate","xml","jaxb","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"}