2

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

why would hibernate entities inned to be Serializable move your application ?
For example, Client <=> Server, since they are the domain object ?? And a little edit on my post too.
I understand that. My query was specific to hibernate related operations.
Consider a small example : Without serialization, how can Domain objects Id ex :(setId(long)) Is saving as a primary key in your database ?? If your object is not Serializable, java won't give that information to underlying database.
Well, that's true about the keys (primary, composite) but not the entities in general.
|
2
  • 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.