Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Hibernate table classes need to be Serializable?

I have inherited a Websphere Portal project that uses Hibernate 3.0 to connect to a SQL Server database.

There are about 130 Hibernate table classes in this project. They all implement Serializable. None of them declare a serialVersionUID field, so the Eclipse IDE shows a warning for all of these classes.

Is there any actual need for these classes to implement Serializable?
If so, is there any tool to add a generated serialVersionUID field to a large number of classes at once (just to make the warnings go away) ?

like image 412
Scott Leis Avatar asked Apr 28 '10 01:04

Scott Leis


People also ask

Do entities class need to be Serializable?

If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface. In practice, if our object is to leave the domain of the JVM, it'll require serialization. Each entity class consists of persistent fields and properties.

Is it necessary to implement Serializable?

If you want a class object to be serializable, all you need to do it implement the java. io. Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement.

How is serialization used in Hibernate?

Hibernate/JPA does not use Serialization as part of the normal CRUD mechanism. But if you want to be able to transfer JPA Entities between JVMs, then they must be Serializable, just like any other transferrable object.

What will happen if parent class is not Serializable?

Case 2: If a superclass is not serializable, then subclass can still be serialized. Even though the superclass doesn't implement a Serializable interface, we can serialize subclass objects if the subclass itself implements a Serializable interface.


2 Answers

Is there any actual need for these classes to implement Serializable?

The JPA spec (JSR 220) summarizes it pretty well (the same applies to Hibernate):

2.1 Requirements on the Entity Class

(...)

If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface.

So, strictly speaking, this is not a requirement unless you need detached entities to be sent over the wire to another tier, to be migrated to another cluster node, to be stored in the HTTP session, etc.

If so, is there any tool to add a generated serialVersionUID field to a large number of classes at once

I think that you could batch this with the Serial version (Ant) Tasks.

like image 92
Pascal Thivent Avatar answered Oct 06 '22 02:10

Pascal Thivent


Hibernate does not require serializable at all.

like image 44
bmargulies Avatar answered Oct 06 '22 04:10

bmargulies