views:

298

answers:

5

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) ?

A: 

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.

Uri
A: 

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.

Chris Nava
+1  A: 

Hibernate does not require serializable at all.

bmargulies
+3  A: 

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.

Pascal Thivent
I think this answer is the most informative. I've elected to leave these classes serializable, since I don't yet understand all the code and am not sure what might be done with them. I'll probably just disable the warning.
Scott Leis
In other words, the spec being cited says, 'if it needs to be Serializable (for reasons other than Hibernate), it needs to be Serializable'. Typical Hibernate usage doesn't involve Serializable.
bmargulies
A: 

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.

matt b