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 Hibernate 6's Jackson 2 JSON FormatMapper: toString() calls objectMapper.writerFor(type).writeValueAsString(value) and wraps JsonProcessingException as IllegalArgumentException('Could not serialize object of java type: <type>') with the cause attached. The Java value of a JSON-mapped attribute could not be rendered to JSON, typically at flush time.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/format/jackson/JacksonJsonFormatMapper.java:86
}
@Override
public <T> T fromString(CharSequence charSequence, Type type) {
try {
return objectMapper.readValue( charSequence.toString(), objectMapper.constructType( type ) );
}
catch (JsonProcessingException e) {
throw new IllegalArgumentException( "Could not deserialize string to java type: " + type, e );
}
}
@Override
public <T> String toString(T value, Type type) {
try {
return objectMapper.writerFor( objectMapper.constructType( type ) ).writeValueAsString( value );
}
catch (JsonProcessingException e) {
throw new IllegalArgumentException( "Could not serialize object of java type: " + type, e );
}
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Read the cause JsonProcessingException — it names the offending property and exact reason
- Register the needed modules on a custom ObjectMapper and inject it via new JacksonJsonFormatMapper(mapper) + hibernate.type.json_format_mapper
- Fix the DTO: add getters/records, break cycles (@JsonIgnore, @JsonManagedReference/@JsonBackReference), detach or initialize lazy proxies before storing
- Add a round-trip integration test for every JSON-mapped entity so serializer gaps surface in CI
Example fix
// before: flush fails 'Could not serialize object of java type: Event'
// cause: Java 8 date/time type `java.time.Instant` not supported by default
@JdbcTypeCode(SqlTypes.JSON)
private EventPayload payload; // contains Instant fields
// after
ObjectMapper om = new ObjectMapper()
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
properties.put(AvailableSettings.JSON_FORMAT_MAPPER, () -> new JacksonJsonFormatMapper(om)); Defensive patterns
Strategy: try-catch
Validate before calling
static boolean serializableWithJackson2(Object value, com.fasterxml.jackson.databind.ObjectMapper mapper) {
try {
mapper.writeValueAsString(value);
return true;
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
return false;
}
} 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")) {
com.fasterxml.jackson.databind.JsonMappingException cause =
(com.fasterxml.jackson.databind.JsonMappingException) ex.getCause();
// cause.getPath() lists the exact property chain that failed
throw new MappingConfigurationException("JSON value not serializable: " + cause.getPathReference(), ex);
}
throw ex;
} Prevention
- Register JavaTimeModule (jackson-datatype-jsr310) on the mapper Hibernate uses when storing java.time fields as JSON
- Give DTOs visible getters or use records; break cycles with @JsonIgnore
- Round-trip test every JSON-mapped entity before release
When it happens
Trigger: Persisting/updating a @JdbcTypeCode(SqlTypes.JSON) attribute whose value Jackson 2 cannot serialize: java.time types without JavaTimeModule ('Java 8 date/time type not supported by default'), no serializer for a private-fielded type (InvalidDefinitionException), cyclic object graph, or an uninitialized Hibernate proxy inside the value.
Common situations: Adding Instant/LocalDateTime/Duration fields to a JSON DTO without registering jackson-datatype-jsr310 on Hibernate's mapper; DTOs with only private fields and no getters; lazy associations serialized while detached; bidirectional references.
Related errors
- Could not serialize object of java type: {}
- Could not deserialize string to java type: {}
- Could not serialize object of java type: {}
- Could not deserialize string to java type: {}
- Could not serialize object of java type: {}
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/350bc02217e4ea0d.
Report an issue: GitHub.