Do Hibernate table classes need to be Serializable?
JavaHibernateOrmSerializableJava Problem Overview
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) ?
Java Solutions
Solution 1 - Java
> 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.
Solution 2 - Java
Hibernate does not require serializable at all.
Solution 3 - Java
Eclipse's "Clean Up" command can do that. However, it also does a lot more so test it and refine the cleanup and format settings before using it on an entire package.
Solution 4 - Java
I'm not familiar with a tool that would do it automatically for a bunch of classes, though you could write a script.
If you mechanically do it with Eclipse, it shouldn't take you more than 4-5 seconds per class which might still be faster than a script.
Solution 5 - Java
>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) ?
You can disable this warning within the Compiler settings for Eclipse - either for the project or your workspace as a whole.