{"record":{"id":"c866d54fc5692472","repo":"hibernate/hibernate-orm","slug":"could-not-serialize","errorCode":null,"errorMessage":"could not serialize","messagePattern":"could not serialize","errorType":"exception","errorClass":"SerializationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java","lineNumber":112,"sourceCode":"\tpublic static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException {\n\t\tif ( outputStream == null ) {\n\t\t\tthrow new IllegalArgumentException( \"The OutputStream must not be null\" );\n\t\t}\n\n\t\tif ( CORE_LOGGER.isTraceEnabled() ) {\n\t\t\tif ( Hibernate.isInitialized( obj ) ) {\n\t\t\t\tCORE_LOGGER.tracev( \"Starting serialization of object [{0}]\", obj );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tCORE_LOGGER.trace( \"Starting serialization of [uninitialized proxy]\" );\n\t\t\t}\n\t\t}\n\n\t\ttry ( var out = new ObjectOutputStream( outputStream ) ) {\n\t\t\tout.writeObject( obj );\n\t\t}\n\t\tcatch (IOException ex) {\n\t\t\tthrow new SerializationException( \"could not serialize\", ex );\n\t\t}\n\t}\n\n\t/**\n\t * Serializes an object to a byte array for storage or\n\t * externalization.\n\t *\n\t * @param obj the object to serialize to bytes\n\t *\n\t * @return a byte[] with the converted Serializable\n\t *\n\t * @throws SerializationException (runtime) if the serialization fails\n\t */\n\tpublic static byte[] serialize(Serializable obj) throws SerializationException {\n\t\tfinal var byteArrayOutputStream = new ByteArrayOutputStream( 512 );\n\t\tserialize( obj, byteArrayOutputStream );\n\t\treturn byteArrayOutputStream.toByteArray();\n\t}","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java#L94-L130","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\npublic class SessionData implements Serializable {\n    private Connection jdbcConnection; // NotSerializableException -> \"could not serialize\"\n}\n\n// after\npublic class SessionData implements Serializable {\n    private transient Connection jdbcConnection;\n    private String connectionUrl; // serializable descriptor instead\n}","handlingStrategy":"try-catch","validationCode":"static void requireSerializable(Object obj) {\n    if (obj != null && !(obj instanceof java.io.Serializable)) {\n        throw new IllegalArgumentException(\"Not serializable: \" + obj.getClass().getName());\n    }\n}","typeGuard":"static boolean isSerializable(Object obj) {\n    return obj == null || obj instanceof java.io.Serializable;\n}","tryCatchPattern":"try {\n    byte[] bytes = SerializationHelper.serialize(obj);\n} catch (org.hibernate.type.SerializationException e) {\n    if (e.getCause() instanceof java.io.NotSerializableException nse) {\n        // nse.getMessage() names the offending class; make it Serializable or mark the field transient\n    }\n}","preventionTips":["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."],"tags":["hibernate","serialization","java-io","not-serializable"],"backgroundTag":"not-serializable-exception","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}