๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-02-18 15:10:40.100 ERROR 8842 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize] with root cause org.hibernate.type.SerializationException: could not deserialize at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:135)

โšก Quick Fix Works 80% of the time

Ensure the Java class stored in the database column implements `java.io.Serializable`.

public class UserPreferences implements Serializable { private static final long serialVersionUID = 1L; // fields... }

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

You mapped a database column (usually a BLOB or JSON type) to a custom Java object in your Entity. When Hibernate tried to read that column from the database, it attempted to convert the raw bytes back into the Java object. Because the Java class does not implement `Serializable`, the JVM refused to create the object.

The HITEC City Parking Spot Analogy:

Imagine packing your belongings into a sealed box (BLOB) for shipping. When the box arrives at the new house, the mover (Hibernate) tries to unpack it, but the items inside are fragile and require special handling (Serializable). Because they aren't marked as safe to handle, the mover refuses to open the box.

๐Ÿ” How to Reproduce Confirm this is your error

Create an Entity with a field of type `MyCustomObject`. Do NOT make `MyCustomObject` implement `Serializable`. Save a record. Try to fetch it using `repo.findById()`.

๐Ÿ› ๏ธ Solutions (5 Ways to Fix)

Solution 1โœ“ Most common cause

Make the custom class implement Serializable

๐Ÿ‘‰ Use this if you must store a complex Java object directly in a DB column.

Java serialization requires the class to implement `Serializable`. Add `implements Serializable` and a `serialVersionUID`.

import java.io.Serializable; public class UserPreferences implements Serializable { private static final long serialVersionUID = 1L; private String theme; private boolean notifications; }
Solution 2

Use a JPA Attribute Converter (JSON)

๐Ÿ‘‰ Use this if you want to store the object as JSON text instead of a binary BLOB.

Instead of Java serialization, write a converter that turns the object into a JSON string before saving, and parses it back when loading.

@Converter(autoApply = true) public class JsonConverter implements AttributeConverter<UserPreferences, String> { // implement convertToDatabaseColumn and convertToEntityAttribute using Jackson }
Solution 3

Change the column type to String

๐Ÿ‘‰ Use this if you don't need a complex object, just raw text.

If the data is just JSON text, map the column to a `String` in your Entity and parse it manually in your service layer.

@Entity public class User { @Column(columnDefinition = "TEXT") private String preferencesJson; // Store as String, not Object }
Solution 4

Ensure the class is on the classpath

๐Ÿ‘‰ Use this if you refactored or renamed the custom class.

If the object was serialized as `com.devinhyderabad.OldPreferences`, but you renamed it to `NewPreferences`, Hibernate can't find the class to deserialize into. Keep the old class or use a converter.

// Ensure the class name and package match exactly what was serialized. // If renamed, you need a custom deserializer or manually migrate the DB data.
Solution 5

Check for serialVersionUID mismatch

๐Ÿ‘‰ Use this if you changed the fields of the custom class after data was already saved.

If you added a new field to the class, the `serialVersionUID` changes. Old bytes in the DB won't match the new class. Set an explicit `serialVersionUID` and handle nulls.

public class UserPreferences implements Serializable { private static final long serialVersionUID = 1L; // Fix this version // ... }

๐Ÿ“‹ Version Notes

Spring Boot 2.x

Uses Java serialization for non-standard types.

Spring Boot 3.x

Identical behavior.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Never use Java serialization for database persistence. It is brittle and non-portable. Always use JSON converters (`@Convert`) for complex objects in database columns.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase