-2

I have a class marked Serializable i.e. this class implements Serializable interface. This class has some variables of type Serializable. I am unable to understand that why these variables are of Serializable type.I know about the Serializable Interface. Please make this concept clear. It will be a great help. My class is-

public class DestinationGroups  implements java.io.Serializable {


 private int id;
 private Serializable name;// why serializable here??
 private Serializable description;// why serializable here??
 private BigDecimal rating;
 private Set destinationses = new HashSet(0);
 private Set destinationGroupRatingses = new HashSet(0);

 public DestinationGroups() {
 }


 public DestinationGroups(int id) {
    this.id = id;
 }

 ..........other code..............
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Vivek Mishra
  • 1,772
  • 1
  • 17
  • 37
  • 2
    If you wrote the class and don't know why you defined the variables to be `Serializable` who should know it? – Thomas Aug 25 '15 at 12:07
  • I didn't write the class. This is auto generated class by hibernate. – Vivek Mishra Aug 25 '15 at 12:08
  • 3
    What is it auto-generated from? A database schema? How does it define the columns in question? I agree that one would expect something like `String` or `BigDecimal` instead. – Thilo Aug 25 '15 at 12:09
  • As for the need to have serializable fields: if you want to serialize a class and its fields the fields must be serializable as well. Primitives are serializable by default while classes such as `BigDecimal` etc. implement `Serializable`. – Thomas Aug 25 '15 at 12:09
  • @Thilo yes this class is generated from a my database table where name and description fields are of nvarchar(max) type. – Vivek Mishra Aug 25 '15 at 12:13
  • 1
    What prior research have you done? If you "know" about the Serializiable interface; what prevented you from searching the internet? Why do you ask us for explanations; if you could study the whole topic ... all by yourself? (stackoverlfow doesn't exist to **teach**, it exists to help with specific problems). – GhostCat Aug 25 '15 at 12:15
  • I am asking for this specific problem I searched the Internet and I only found the definition of Transient variables not Serializable variables. That's why I asked this question here. – Vivek Mishra Aug 25 '15 at 12:17
  • @Jagermeister should I write all the search urls in my question? – Vivek Mishra Aug 25 '15 at 12:18
  • Well, when I run google; one of the very first links is http://www.tutorialspoint.com/java/java_serialization.htm ... which explains in great detail what Serialization is about ... – GhostCat Aug 25 '15 at 12:28

3 Answers3

4

@JordiCastilla's answer explains what Serializable is, but not why it appears here.

The real problem here is Hibernate's default mapping for NVARCHAR. In recent versions (i.e. Hibernate 4.2 and later) NVARCHAR is mapped to String. In older versions, Hibernate maps NVARCHAR to Serializable.

If you are generating your Java code using the Hibernate Code Generation Plugin, then the solution is to edit your plugin's "hibernate.reveng.xml" as described here:

On the other hand, if you are having problems with Hibernate mapping NVARCHAR incorrectly at runtime, then this Q&A might help:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

What is serialization?
Serialization basically is the conversion of an object to bytes, that makes this object easily saved, shared or streamed.

Why attributes in class must be serializable?
That means, all components in a Serializable object MUST be serializables itselfs or you will be breaking Serializable contract.

Find here and here further Serialization info.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • The real question (at least for me) is why nvarchar(max) does not get turned into String. – Thilo Aug 25 '15 at 12:30
-1

If you want to serialize a class all of it's properties has to be Serializable. With this usage you guarantee to make all properties Serializable. If somehow name is an object which isn't serializable you won't be able to serialize whole class.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
ibrahimbayer
  • 262
  • 1
  • 9