hibernate/hibernate-orm · error · SerializationException
could not serialize
Error message
could not serialize
What it means
serialize() hands the object to ObjectOutputStream.writeObject; any IOException from the JDK is wrapped into SerializationException with this message. The dominant cause is NotSerializableException for the object itself or something reachable from a non-transient field, though any serialization-time IO failure on the destination stream lands here too. The original IOException is preserved as the cause and names the offending class.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java:112
public static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException {
if ( outputStream == null ) {
throw new IllegalArgumentException( "The OutputStream must not be null" );
}
if ( CORE_LOGGER.isTraceEnabled() ) {
if ( Hibernate.isInitialized( obj ) ) {
CORE_LOGGER.tracev( "Starting serialization of object [{0}]", obj );
}
else {
CORE_LOGGER.trace( "Starting serialization of [uninitialized proxy]" );
}
}
try ( var out = new ObjectOutputStream( outputStream ) ) {
out.writeObject( obj );
}
catch (IOException ex) {
throw new SerializationException( "could not serialize", ex );
}
}
/**
* Serializes an object to a byte array for storage or
* externalization.
*
* @param obj the object to serialize to bytes
*
* @return a byte[] with the converted Serializable
*
* @throws SerializationException (runtime) if the serialization fails
*/
public static byte[] serialize(Serializable obj) throws SerializationException {
final var byteArrayOutputStream = new ByteArrayOutputStream( 512 );
serialize( obj, byteArrayOutputStream );
return byteArrayOutputStream.toByteArray();
}View on GitHub (pinned to fad1729dce)
Solutions
- Read getCause(): NotSerializableException names the exact offending class — make it implement Serializable or mark the field transient.
- For third-party types you cannot modify, store a serializable projection (a DTO) or switch the column format to JSON.
- If the cause is a genuine IO error (disk, channel), fix the destination resource rather than the object graph.
Example fix
// before
public class SessionData implements Serializable {
private Connection jdbcConnection; // NotSerializableException -> "could not serialize"
}
// after
public class SessionData implements Serializable {
private transient Connection jdbcConnection;
private String connectionUrl; // serializable descriptor instead
} Defensive patterns
Strategy: try-catch
Validate before calling
static void requireSerializable(Object obj) {
if (obj != null && !(obj instanceof java.io.Serializable)) {
throw new IllegalArgumentException("Not serializable: " + obj.getClass().getName());
}
} Type guard
static boolean isSerializable(Object obj) {
return obj == null || obj instanceof java.io.Serializable;
} Try / catch
try {
byte[] bytes = SerializationHelper.serialize(obj);
} catch (org.hibernate.type.SerializationException e) {
if (e.getCause() instanceof java.io.NotSerializableException nse) {
// nse.getMessage() names the offending class; make it Serializable or mark the field transient
}
} Prevention
- Audit every new field of serialized classes for serializability.
- Mark infrastructure references (connections, streams, services) transient.
- Add a round-trip (serialize then deserialize) unit test for every class persisted as a blob.
- Prefer JSON for stored blobs whose shape changes often.
When it happens
Trigger: Serializing an object whose class (or one of its fields' classes) does not implement java.io.Serializable; graphs containing non-serializable runtime types such as Connection, Socket, or InputStream; IO failures when the destination stream breaks mid-write (disk full, closed channel).
Common situations: Entities or DTOs stored as serialized blobs (SERIALIZED varbinary columns) that gained a non-serializable field; objects wrapping JDBC or service references; third-party value types without Serializable; cache stores that serialize detached state after a refactor.
Related errors
- Unable to deserialize from cached file [%s]
- Cannot serialize Session while connected
- Blobs may not be accessed after serialization
- Clobs may not be accessed after serialization
- Cannot serialize an EntityUniqueKey which represents a non s
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/c866d54fc5692472.
Report an issue: GitHub.