hibernate/hibernate-orm · error · IllegalArgumentException
Could not serialize object of java type: {}
Error message
Could not serialize object of java type: {} What it means
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).
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/format/jackson/JacksonXmlFormatMapper.java:235
collectionWrapper = new CollectionWrapper<>( list );
}
return writeValueAsString(
collectionWrapper,
javaType,
new ParameterizedTypeImpl( CollectionWrapper.class,
new Type[] {javaType.getJavaTypeClass().getComponentType()}, null )
);
}
}
return writeValueAsString( value, javaType, javaType.getJavaType() );
}
private <T> String writeValueAsString(Object value, JavaType<T> javaType, Type type) {
try {
return objectMapper.writerFor( objectMapper.constructType( type ) ).writeValueAsString( value );
}
catch (JsonProcessingException e) {
throw new IllegalArgumentException( "Could not serialize object of java type: " + javaType, e );
}
}
@JacksonXmlRootElement(localName = "Collection")
public static class CollectionWrapper<E> {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "e")
Collection<E> value;
public CollectionWrapper() {
this.value = new ArrayList<>();
}
public CollectionWrapper(Collection<E> value) {
this.value = value;
}
}
View on GitHub (pinned to fad1729dce)
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).
Example fix
// before - non-String map keys cannot be serialized to XML @JdbcTypeCode(SqlTypes.SQLXML) Map<UUID, String> items; // IllegalArgumentException on flush // after - String keys render fine @JdbcTypeCode(SqlTypes.SQLXML) Map<String, String> items;
Defensive patterns
Strategy: validation
Validate before calling
// Verify the attribute value renders as XML before it reaches Hibernate
static void assertXmlSerializable(Object value) {
try {
XmlMapper xml = XmlMapper.builder().findAndAddModules().build();
xml.writeValueAsString( value );
} catch ( JsonProcessingException e ) {
throw new IllegalStateException( "Value will fail on flush: " + e.getOriginalMessage(), e );
}
} Try / catch
try {
tx.commit();
} catch ( IllegalArgumentException e ) {
if ( e.getCause() instanceof com.fasterxml.jackson.core.JsonProcessingException jpe ) {
// e.g. non-String map keys - fix the attribute structure
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Could not deserialize string to java type: {}
- Could not serialize object of java type: {}
- Could not serialize object of java type: {}
- Could not serialize object of java type: {}
- Could not deserialize string to java type: {}
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/5c4931571e953cd5.
Report an issue: GitHub.