akash777.sharma
akash777.sharma

Reputation: 702

Should we implement Serializable interface while making Domain Class in hibernate

I am new in hibernate.I tried some basic CRUD operations using hibernate API.

I created a class Person

@Entity
class Person
{
   String name
}

I was able to save this class in Database. Till now I was thinking that JPA internally makes this class implement Serializable because only serialization can save the state of an object.But I tried this :

Person p=new Person();
boolean bool=p instanceof Serializable;    
sop(bool);   //false

Then I created another class Human (found this way of implementation on Hibernate doc)

@Entity
class Human implements Serializable
{
    String name    
}

Human h=new Human();
boolean bool=h instanceof Serializable    
sop(bool);  //true

Which way we should create our domain classes and how hibernate internally treats these two ways ?

Please help.

Upvotes: 2

Views: 3847

Answers (1)

fujy
fujy

Reputation: 5264

If your entities are going to be transfered over a network, to be stored in an HTTP session, or even to be stored in a file on your hard disk then they must implement Serializable. Otherwise Hibernate has nothing to do with Serialization, check more here

Do Hibernate table classes need to be Serializable?

Beans serialization in JSP

Upvotes: 6

Related Questions