{"record":{"id":"cf4336e2de84a297","repo":"hibernate/hibernate-orm","slug":"cannot-serialize-an-entityuniquekey-which-represen","errorCode":null,"errorMessage":"Cannot serialize an EntityUniqueKey which represents a non serializable property value [","messagePattern":"Cannot serialize an EntityUniqueKey which represents a non serializable property value \\[","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/spi/EntityUniqueKey.java","lineNumber":98,"sourceCode":"\t\t\t\t&& keyType.isEqual( that.key, key );\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"EntityUniqueKey\" + MessageHelper.infoString( entityName, uniqueKeyName, key );\n\t}\n\n\tprivate void writeObject(ObjectOutputStream oos) throws IOException {\n\t\tcheckAbilityToSerialize();\n\t\toos.defaultWriteObject();\n\t}\n\n\tprivate void checkAbilityToSerialize() {\n\t\t// The unique property value represented here may or may not be\n\t\t// serializable, so we do an explicit check here in order to generate\n\t\t// a better error message\n\t\tif ( key != null && !Serializable.class.isAssignableFrom( key.getClass() ) ) {\n\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\"Cannot serialize an EntityUniqueKey which represents a non \" +\n\t\t\t\t\t\t\t\"serializable property value [\" + entityName + \".\" + uniqueKeyName + \"]\"\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Custom serialization routine used during serialization of a\n\t * Session/PersistenceContext for increased performance.\n\t *\n\t * @param oos The stream to which we should write the serial data.\n\t *\n\t */\n\tpublic void serialize(ObjectOutputStream oos) throws IOException {\n\t\tcheckAbilityToSerialize();\n\t\toos.writeObject( uniqueKeyName );\n\t\toos.writeObject( entityName );\n\t\toos.writeObject( key );","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityUniqueKey.java#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\npublic class SkuCode {   // used as unique-key property value\n    private final String code;\n    public SkuCode(String code) { this.code = code; }\n}\n\n// after\npublic class SkuCode implements java.io.Serializable {\n    private final String code;\n    public SkuCode(String code) { this.code = code; }\n    @Serial private void readObjectNoData() {}\n}","handlingStrategy":"type-guard","validationCode":"Object key = /* unique-key property value before triggering loads */ null;\nif (key != null && !(key instanceof java.io.Serializable)) {\n    throw new IllegalStateException(\"Unique-key value of type \" + key.getClass().getName() + \" is not Serializable; cannot passivate session\");\n}","typeGuard":"static boolean isSerializableUniqueKeyValue(Object v) {\n    return v == null || v instanceof java.io.Serializable;\n}","tryCatchPattern":"try (ObjectOutputStream oos = new ObjectOutputStream(out)) {\n    oos.writeObject(session);\n} catch (IllegalStateException e) {\n    if (String.valueOf(e.getMessage()).contains(\"non serializable property value\")) {\n        // clear the persistence context or detach entities, then retry serialization\n        session.clear();\n    } else {\n        throw e;\n    }\n}","preventionTips":["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"],"tags":["hibernate","serialization","unique-key","persistence-context","session-passivation"],"backgroundTag":"non-serializable-entity-key","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}