hibernate/hibernate-orm · error · IllegalStateException
Cannot serialize an EntityUniqueKey which represents a non s
Error message
Cannot serialize an EntityUniqueKey which represents a non serializable property value [
What it means
EntityUniqueKey is the persistence-context key for entities referenced through a unique-key property instead of the primary key (e.g. @ManyToOne pointing at a unique non-PK column / legacy property-ref). Before serializing, writeObject() runs checkAbilityToSerialize(), which requires the referenced property value to implement Serializable and otherwise throws IllegalStateException with the offending entityName.uniqueKeyName.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/engine/spi/EntityUniqueKey.java:98
&& keyType.isEqual( that.key, key );
}
@Override
public String toString() {
return "EntityUniqueKey" + MessageHelper.infoString( entityName, uniqueKeyName, key );
}
private void writeObject(ObjectOutputStream oos) throws IOException {
checkAbilityToSerialize();
oos.defaultWriteObject();
}
private void checkAbilityToSerialize() {
// The unique property value represented here may or may not be
// serializable, so we do an explicit check here in order to generate
// a better error message
if ( key != null && !Serializable.class.isAssignableFrom( key.getClass() ) ) {
throw new IllegalStateException(
"Cannot serialize an EntityUniqueKey which represents a non " +
"serializable property value [" + entityName + "." + uniqueKeyName + "]"
);
}
}
/**
* Custom serialization routine used during serialization of a
* Session/PersistenceContext for increased performance.
*
* @param oos The stream to which we should write the serial data.
*
*/
public void serialize(ObjectOutputStream oos) throws IOException {
checkAbilityToSerialize();
oos.writeObject( uniqueKeyName );
oos.writeObject( entityName );
oos.writeObject( key );View on GitHub (pinned to fad1729dce)
Solutions
- Make the unique-key property's value type implement java.io.Serializable
- Rework the association to reference the primary key (or a Serializable key) instead of a non-serializable unique property
- If a custom UserType produces the value, have it return a Serializable representation
Example fix
// before
public class SkuCode { // used as unique-key property value
private final String code;
public SkuCode(String code) { this.code = code; }
}
// after
public class SkuCode implements java.io.Serializable {
private final String code;
public SkuCode(String code) { this.code = code; }
@Serial private void readObjectNoData() {}
} Defensive patterns
Strategy: type-guard
Validate before calling
Object key = /* unique-key property value before triggering loads */ null;
if (key != null && !(key instanceof java.io.Serializable)) {
throw new IllegalStateException("Unique-key value of type " + key.getClass().getName() + " is not Serializable; cannot passivate session");
} Type guard
static boolean isSerializableUniqueKeyValue(Object v) {
return v == null || v instanceof java.io.Serializable;
} Try / catch
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(session);
} catch (IllegalStateException e) {
if (String.valueOf(e.getMessage()).contains("non serializable property value")) {
// clear the persistence context or detach entities, then retry serialization
session.clear();
} else {
throw e;
}
} Prevention
- Make every type used as an association key (unique-key property, composite id parts) implement Serializable
- Prefer PK-based associations over unique-key (property-ref) mappings
- If sessions can be passivated, keep persistence contexts small (clear after unit of work) to reduce what must serialize
When it happens
Trigger: Serializing a Session/PersistenceContext (or a lazy proxy keeping the session) that contains an entity loaded via a unique-key association whose key property value's class does not implement java.io.Serializable; also hit via the custom serialization routine used when a Session is written to a stream.
Common situations: HTTP session passivation with Open Session in View; Spring Session / Wicket serializing a session holding an open persistence context; distributing sessions or proxies to a compute grid or cache; associations keyed by a custom value type or record that was never made Serializable.
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
- Given entity is not associated with the persistence context
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/cf4336e2de84a297.
Report an issue: GitHub.