13

Possible Duplicates:
Do Hibernate table classes need to be Serializable?
What does Serializable mean?

public class ExampleEntity implements Serializable
{
     @Id
     private long id;
     private int fieldInt;
     private long fieldLong;
     private String fieldString;
 }

I am looking after JPA tutorials. I am able to understand the basic concepts, but in all the tutorials they have added, this serializable. What is the purpose of this? How does that help me? Any suggestions Please?

Community
  • 1
  • 1
IamIronMAN
  • 1,871
  • 6
  • 22
  • 28
  • This is either a dupe of [Do Hibernate table classes need to be Serializable?](http://stackoverflow.com/questions/2726300/do-hibernate-table-classes-need-to-be-serializable) or, if not an Hibernate/JPA specific question, a dupe of [What is object serialization?](http://stackoverflow.com/questions/447898/what-is-object-serialization). In both cases, it's a dupe :) – Pascal Thivent Sep 20 '10 at 16:37
  • I edited the tags. Removed "jpa" and "hibernate" and added "serialization". – pakore Sep 20 '10 at 16:39
  • @pascal : sorry am a newbie. I am learning now. i will avoid this in future certainly. – IamIronMAN Sep 20 '10 at 16:42
  • @Code Oh, don't worry, no problem. It's just that the answer is already there and in such cases, the practice is to close the question (as duplicate) to avoid a dilution of the information. But doing a little search first is a good idea :) – Pascal Thivent Sep 20 '10 at 16:48
  • @pascal thanks for your concerns and its correct. U all help us to grow. thanks – IamIronMAN Sep 20 '10 at 16:50
  • Adding another more obvious dupe: [What does Serializable mean?](http://stackoverflow.com/questions/3429921/what-does-serializable-mean) – Pascal Thivent Sep 22 '10 at 17:46

2 Answers2

26

It means that the class can be serialized. This means that it can be converted into an stream of 0's and 1's and be sent to other service, like a webservice or and ORM (hibernate) or whatever. Then this service knows how store the stream.

Also, your program can receive a serialized class and create an instance for it from the stream of 0's and 1's.

It's the way to "save" instances of classes and "restore" them at any other moment in time.

More info at

To be able to make a class serializable, you must implement the interface "Serializable".

  • Why so? Because when the moment to serialize a class arrive, the function writeObtject will be called to convert the object into bytes. On the other hand, when you have the bytes and you want to get the instance of the class, the function readObject will be called.

  • Why is not automatic? A variable value it's already represented by bytes. Because the problem comes for example when a class holds instances of other classes. What do you do? You serialize the other classes as well? You just keep the reference address? It's pretty complex and it may be dependant on your type of project and design.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
pakore
  • 11,395
  • 12
  • 43
  • 62
  • @pokore thanks i understood well. also what does this @GeneratedValue(strategy=GenerationType.AUTO) mean? is it for auto increment of the ID. – IamIronMAN Sep 20 '10 at 16:31
  • 1
    @weed :-) That's a completely different question – Sean Patrick Floyd Sep 20 '10 at 16:32
  • 1
    To clarify further, the point of the interface is to mark classes that can sensibly be serialized. For example, it wouldn't make sense to serialize a live network socket, so it's not marked as serializable. – Matti Virkkunen Sep 20 '10 at 16:34
  • 1
    @Code 'n' weed. It is a different question, but I'll answer it anyway. With that annotation you are telling your JPA Entity (tipically Hibernate) that you let it generate the id automatically. For example, it is useful when you are creating instances of `Image.class` class. But in other cases you may want to generate the id yourself, for example in a `User.class` where the id could be the username. – pakore Sep 20 '10 at 16:35
  • @pakore. thanks i understood the basics very well because of u all. mainly you. If u don mind Can u please say a nice material to study JPA and i am using hibernate provider.Am struggling – IamIronMAN Sep 20 '10 at 16:44
  • 1
    @Code 'N'Weed, I recommend you the book "Java Persistence with Hibernate", by Christian Bauer and Gaving King. That is considered by many the bible of hibernate. – pakore Sep 21 '10 at 07:25
  • @pakore thank u very much its reaaly good am starting with that. – IamIronMAN Sep 21 '10 at 08:09
  • And what if it's not serialized? My program still works fine even though nothing implements serializable – CodyBugstein Jun 02 '14 at 07:46
1

Serializable classes can be serialized - converted into bytes for storing on a hard drive or transmitting over a network.

fredley
  • 32,953
  • 42
  • 145
  • 236