Do I have to use Serializable for JPA and Hibernate entities?


Reading through Serialization chapter from Effective Java, I realized I still have some Serializables here and there in my codebase (Spring Boot web app -> Spring Data -> Hibernate). The recommendation is to avoid serialization as much as one can.

Do I still need to implement Serializable?
What are the reasons for doing so in Hibernate?
If I do so, how can it be exploited?


public class AbstractEntityWithVersion<ID extends Serializable> implements Identifiable<ID> {

    private final ID id;

    @Column(name = "REC_VERSION", nullable = false)
    private Long version;

    protected AbstractEntityWithVersion() { = null;
public interface Identifiable<ID extends Serializable> {
	ID getId();

Why does ID in this example have to be Serializable?


Entities do not need to be Serializable. However, if you want to serialize them, you can safely implement Serializable.

The identifier has to be Serializable because that’s a JPA requirement since the identifier might be use as the key for a second-level cache entry.