File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

is primary key necessary

Hari patnaik

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

Paul Sturrock

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

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.

[OCA 8 Book] [Blog]
Paul Sturrock

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:
subject: is primary key necessary
It's not a secret anymore!