views:

148

answers:

4

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?

A: 

See Serialization

Galwegian
+6  A: 

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.

pakore
@pokore thanks i understood well. also what does this @GeneratedValue(strategy=GenerationType.AUTO) mean? is it for auto increment of the ID.
Code 'N' Weed
@weed :-) That's a completely different question
seanizer
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
@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
@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
Code 'N' Weed
@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
@pakore thank u very much its reaaly good am starting with that.
Code 'N' Weed
+1  A: 

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

fredley
@fridley thanks for ur explanation.
Code 'N' Weed
+2  A: 

Some Links:

seanizer
@seanizer thanks for editing and answering..
Code 'N' Weed
sure, no prob..
seanizer
@seanizer Can u please say a nice material to study JPA and i am using hibernate provider.
Code 'N' Weed
can't really tell you much but to google "hibernate jpa tutorial" http://www.google.com/search?q=hibernate+jpa+tutorial
seanizer