Discussions

EJB programming & troubleshooting: Why a Primary Key in EJB is a Java Object?

  1. Why a Primary Key in EJB is a Java Object? (3 messages)

    Hi Folks,

    Can some one justify why Sun EJB Specificiation mandates the usage of Java objects which are serializable to be the primary key for an Entity Bean instead of primitives which are serializable,perhaps by default.

    Thanks.

    Regards
    Karthik Banda
  2. Primitives are not serializable on their own. Yes, they get serialized/deserialized automatically, with the object they are used in. So for PK implementation we have to have an object which can be serialized. Hope this helps...

    Vishal
  3. No this does not help me! Why the primary key has to be serializable? Why it cannot be e.g. integer like other integer persistant fields?
  4. Because the PK must be able to 'exist' outside the object. When you use a finder method on the Home interface, you pass in a PK object. Note that this PK object exists before the 'real' object does, and since the object can get transferred over the wire or stored temporarily (what and how exactly depends upon the container implementation) it must be serializable.