user2998826
user2998826

Reputation: 57

Implementing Serializable interface in hibernate entity class

What is the use of implementing Serializable interface in hibernate entity class? That interface continue nothing in that.

Upvotes: 2

Views: 5515

Answers (2)

Santosh
Santosh

Reputation: 17893

  • Not sure why in Hibernate entity classes need to implement Serializable interface. A Serializable POJO is the one which can be written to the disc or transmitted over the wire.
  • If your Hibernate entities or POJOs are involved in any of these then only you need to implement Serializable interface.

EDIT: - Just realized that the keys (primary, composite) needs to be Serializable as they are referred by persistent Session. (Reference)

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

It's Marker Interface and just like an normal interface.

The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.

In Serializable case java

public interface Serializable{
}

and some class

 public class someObje  implements Serializable{

  }

And somewhere else Runtime realizes objects like

 if(someObje instnaceOf Serializable){

  //Hey this object can serialize you know. Grant security permission.

 }

Coming to your question, by definition

Serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

Now without Serialization ,how hibernate entities move in your application (Client <=> Server <=> Database)?

And also to detect the type. For ex in hibernate look at the method signature of Seesion#get() method

Object get(Class clazz,
           **Serializable** id)
           throws HibernateException 

Note:This theory applies not only for hibernate entities where ever Object needs to be serialize.

Upvotes: 3

Related Questions