1

Test Class to serialize is as follows:

 public Class Test implements Serializable {
      private static final long serialVersionUID = GENERATED_LONG_VALUE;
      private int val;
      private SomeClass_1 val_1;
      private SomeClass_2 val_2;
      private SomeClass_3 val_3;
      // getter and setter for above
  }

I have serialized Object of above class as BLOB in table.

Now while deserializing I just want val and val_1. So I have overrided readObject() method in Test class like below.

    private void readObject(java.io.ObjectInputStream stream)
        throws IOException, ClassNotFoundException {

        this.val = (int) stream.readObject();
        this.val_1 = (SomeClass_1) stream.readObject();
    }

But after this also, It is doing stream.readObject() for val_3 and val_4. I am not understanding why it is happening even I am not reading val_3 and val_4 in stream.readObject().

P.S. I am doing serialization on server X, While deserializing it at server Y and class Structure is exactly same at server Y like server X.

Dhairyashil
  • 179
  • 2
  • 5
  • 18
  • 3
    Declare the variables you don't want serialized as [`transient`](http://stackoverflow.com/a/910522/2398375) – Vince Mar 23 '16 at 13:32
  • I have tried that approach also, but it is not working. – Dhairyashil Mar 23 '16 at 13:34
  • @VinceEmigh: While serializing I have used all fields, while deserializing I only want first 2 fields to be restored. not other fields. but still it goes for processing other fields. – Dhairyashil Mar 23 '16 at 13:59
  • 2
    Don't serialize all the fields. It doesn't make sense to serialize them if you don't plan on deserializing them. – Vince Mar 23 '16 at 14:11
  • @VinceEmigh: I need them on `server x`. But at `server Y`, I don't need them. That's why I need to serialize Object with all fields. – Dhairyashil Mar 23 '16 at 14:14
  • @Dhairyashil If `transient` isn't working for you, you did it wrong. Didn't compile the code, didn't deploy it, ... – user207421 Mar 23 '16 at 22:11
  • @EJP: I have cleaned it properly, mvn clean / install also and redeployed it also. But still its giving me that strange scenario. Don't know why it is going to `readObject()`, which I don't want, – Dhairyashil Mar 24 '16 at 16:18

1 Answers1

2

If one Server X requires all members of the object to be passed to it, but Server Y doesn't, you might want to:

  1. Look into Externalization. This gives you full control of marshalling and unmarshalling, which seems like what you need.

  2. Send the servers different proxy objects: One which serializes all fields, and one that doesn't. You could create a proxy for data that is to be sent to the servers, passing in the object you want to send. One proxy uses transient, the other doesnt.

  3. Don't send objects, rather than data required to reconstruct the objects on the other side. This relieves stress on the network if the objects you want to send over don't require much data.

Vince
  • 14,470
  • 7
  • 39
  • 84
  • Can I do serialization using java.io.Serializable and deserialization using java.io.Externalizable? Because I can't delete already serialized data in DB and I want it back at `server Y`. – Dhairyashil Mar 23 '16 at 15:08
  • @Dhairyashil I've never tried it, but I have a feeling that would lead to problems, as `Serializable` uses a specific algorithm to marshall the data, and by not following the same system on both sides of the connection, could lead to leaks. This is an assumpsion, I haven't actually tried it myself, and I couldn't find any articles or documentation on the subject. There could be safegaurds to prevent problems, but the only way to find out, from my perspective, is to try it and monitor the network (if no logical problems were to occur). – Vince Mar 23 '16 at 21:08
  • @Dhairyahil Depending on how much serialized data you are currently managing, you could always read it back in, then marshall it using externalization to ensure your systems are consistent. If it's too much of a problem, feel free to choose the 2nd option I have provided, which is a work-around I feel should help your situation. – Vince Mar 23 '16 at 21:11