Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is primary key necessary

 
Hari patnaik
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I am reading Hibernate docs. I was trying a simple example which is simple select.
My question for you is do we need to have a primary key on every table which is being accessed from Hibernate

Regards
Hari
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a primary key on every entity in a relational database, regardless of whether you use Hibernate or any other tool to access the data. "Relational" data without a unique identifier is not relational.
[ December 06, 2005: Message edited by: Paul Sturrock ]
 
Scott Selikoff
author
Saloon Keeper
Posts: 4028
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not completely true, for tables with small enough number of columns, the primary key can be implied as the row itself. For example, if you have a set of students and a set of classes you may have a table IN_CLASS that just contains a student_id and a class_id. The record itself would be its own primary key.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case it should be defined as a composite key (a composite key is more properly refered to as a "composite primary key"). I'm not suggesting every entity should have a surrogate key (though there are technical reasons which suggest that this might a good idea). But every table in a relational database must have a primary key, otherwise what is stored in the table is not relational data.
[ December 06, 2005: Message edited by: Paul Sturrock ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic