Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

composite primary keys

 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the JPA specifications composite primary keys classes have to be serializable . Can anyone brief why is this condition put?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic