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) ?
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.
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.
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.
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.
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.
Hibernate does not require serializable at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With