hibernate/hibernate-orm · error · IllegalArgumentException

Could not serialize object of java type: {}

Error message

Could not serialize object of java type: {}

What it means

Serialization half of the Jackson 3 XML FormatMapper: writeValueAsString() renders the mapped Java value to XML via xmlMapper.writerFor(type) and wraps any JacksonException as IllegalArgumentException('Could not serialize object of java type: <type>'). The value could not be written as XML at flush time.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/format/jackson/Jackson3XmlFormatMapper.java:233

					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 xmlMapper.writerFor( xmlMapper.constructType( type ) ).writeValueAsString( value );
		}
		catch (JacksonException e) {
			throw new IllegalArgumentException( "Could not serialize object of java type: " + javaType, e );
		}
	}

	@JsonRootName(value = "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

  1. Read the cause JacksonException — it names the property/key that failed
  2. Sanitize element-name sources: map keys and @JacksonXmlProperty localNames must be valid XML names (letters/underscores, no spaces)
  3. Expose getters or use records so the mapper can see the properties; break cycles with @JsonIgnore
  4. Inject a configured XmlMapper via new Jackson3XmlFormatMapper(xmlMapper, legacyFormat) and hibernate.type.xml_format_mapper

Example fix

// before: map keys become XML element names
@JdbcTypeCode(SqlTypes.XML)
private Map<String, String> translations; // key "en-US" -> invalid element name
// after: wrap map entries so keys become attribute values
public static class Entry {
    @JacksonXmlProperty(isAttribute = true)
    public String lang;
    @JacksonXmlText
    public String text;
}
private List<Entry> translations;
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure every map key / property name destined for XML is a valid element name
static boolean isValidXmlElementName(String name) {
    return name != null && !name.isEmpty()
            && (Character.isLetter(name.charAt(0)) || name.charAt(0) == '_')
            && name.chars().allMatch(c -> Character.isLetterOrDigit(c) || c == '_' || c == '-' || c == '.');
}

Try / catch

try {
    session.merge(entity);
    session.flush();
} catch (IllegalArgumentException ex) {
    if (ex.getMessage() != null && ex.getMessage().startsWith("Could not serialize object of java type")) {
        Throwable cause = ex.getCause(); // names the property/key that cannot be written
        throw new MappingConfigurationException("Value not XML-serializable: " + cause.getMessage(), ex);
    }
    throw ex;
}

Prevention

When it happens

Trigger: Persisting an @JdbcTypeCode(SqlTypes.XML) attribute whose value has no XML serializer (missing getters, exotic field type without module), contains a map key or property name that is not a valid XML element name, or builds a cyclic graph the writer cannot resolve.

Common situations: Map<String, ...> fields persisted to XML where keys contain spaces or start with digits; DTO fields renamed without @JacksonXmlProperty; Jackson 2 → Jackson 3 migration changing module registration; lazy proxies inside the XML value.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/0fc6890f3531336f. Report an issue: GitHub.