Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Hibernate and Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate and Serializable " Watch "Hibernate and Serializable " New topic
Author

Hibernate and Serializable

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
why do we require to implement Serializable for persistent classes in hibernate?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Have a look here:

http://docs.oracle.com/cd/E19159-01/819-3669/bnbpz/index.html

Under Requirements for Entity Classes

If an entity instance be passed by value as a detached object, such as through a session bean’s remote business interface, the class must implement the Serializable interface.


In short it is not a requirement, but since this is a common occurrence (HTTP Session, RMI etc) it is a best practice.


[How To Ask Questions][Read before you PM me]
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Thanks Bill for the reply and the link. It might take some time to understand the content of the link since I am new to hibernate.
But one more question, In which condition we should implement Serializable and in which condition it's ok not to implement ?

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Some examples of when serializing would be required is if you are storing detached entities in an HTTP session, passing it over the wire or perhaps you have a multi node cluster. If not you could get away without it but as I said your really should just make them serializable as these types of situations are commonplace and its recommended by the spec that you do so.

So my advice is rather than spending too much time determining if it is strictly required in your case just do it

I do think it worthwhile to understand the concept of serialization and why its necessary, but that question would be more appropriate for the Java in General forum.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Let's put it the other way around: in what circumstances would it be detrimental to your design if your class implemented Serializable?

 
 
subject: Hibernate and Serializable