GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes composite primary keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "composite primary keys" Watch "composite primary keys" New topic
Author

composite primary keys

Ravi Bansal
Ranch Hand

Joined: Aug 18, 2008
Posts: 84
As per the JPA specifications composite primary keys classes have to be serializable . Can anyone brief why is this condition put?


SCJP 5.0 94%
OCE-EJBD 90%
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

If the Hibernate framework ever needs to "passivate" as composite key by saving its state temporarily to the hard drive, or even send the data across a network or io connection, the fact that the bean is serializable means that the framework can perform these tasks by using standard Java mechanisms. If the bean is not serializable, Hibernate cannot depend on the built-in serialization features of the JVM.

-Cameron McKenzie
Ravi Bansal
Ranch Hand

Joined: Aug 18, 2008
Posts: 84
but why only with composite primary key.?
if i have primary key member specifed in a class , which is not serializable...then also hibernate framework works
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

What type of non-serializable instance have you specified as a primary key? And how is Hibernate mapping it to a database column?
Ravi Bansal
Ranch Hand

Joined: Aug 18, 2008
Posts: 84
say a simple primitive type (int ) is specified in the class and its mapped to some field id in the db.

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

An int is a primitive type. The Serializable commitment is essentially that an object is only made up of the basic primitive types, and if there are non-primitive types, those types be either Serializable or be marked as transient.

All the primitive types are essentially 'serializable', as the JVM can write and read them to various output streams. Of course, technically we can't call primitives "Serializable" because they are not object, but they sorta are.

-Cameron McKenzie
 
GeeCON Prague 2014
 
subject: composite primary keys