56

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Scott Leis
  • 2,810
  • 6
  • 28
  • 42
  • There's a case that a table class have to be Serializable http://stackoverflow.com/questions/9271835/why-composite-id-class-must-implement-serializable – Sanghyun Lee May 25 '15 at 05:00
  • I found these two related questions are interesting: https://stackoverflow.com/questions/4525186/cannot-be-cast-to-java-io-serializable, https://stackoverflow.com/questions/2020904/when-and-why-jpa-entities-should-implement-serializable-interface – DanielM Oct 16 '18 at 07:29

5 Answers5

77

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
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 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 Apr 28 '10 at 04:14
  • 2
    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 Apr 28 '10 at 10:51
8

Hibernate does not require serializable at all.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
5

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
  • 6,614
  • 3
  • 25
  • 31
2

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
  • 88,451
  • 51
  • 221
  • 321
2

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
  • 138,234
  • 66
  • 282
  • 345