Discussions

EJB programming & troubleshooting: Serialization coding for value objects

  1. Serialization coding for value objects (3 messages)

    When using value objects, I am aware that they have to implement Serializable.

    1) Do you have to create any other methods on the object to write the stream, or will the container do this automatically?

    2) Why do they have to implement serializable?

    Cheers..
  2. 1) Automagic.
    2) I don't know. I just follow the cookbook.
  3. The answer to your second question lies in RMI. The value objects are passed back and forth between different tiers (presentation/servlet and application/EJB). In J2EE, the tiers could potentially reside in multiple JVMs. RMI allows objects to communicate in memory, across JVMs. It also requires that all objects that are passed as parameters be serializable. Hence, the need for the value objects to implement Serializable.
  4. 1) You don't HAVE to do anything, but if your object is complex, then be aware that reflection will be used to serialize the object, which will be slow.

    Speed it up by over-riding the readObject and writeObject methods and simply spit the objects you contain down the stream you are provided in a given order. Then read them back out the other end in the corresponding order. If you have a complex object you will be amazed at how much faster your machine goes!

    2) Serializable is used to tell the VM that it is OK to pass this object outside your virtual machine. It is a marker interface only, in the same manner as Cloneable.

    Chz

    Tony