aspose file tools*
The moose likes Object Relational Mapping and the fly likes is primary key necessary Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "is primary key necessary" Watch "is primary key necessary" New topic
Author

is primary key necessary

Hari patnaik
Greenhorn

Joined: Apr 13, 2005
Posts: 12
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

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is primary key necessary