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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 3703

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

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
Similar Threads
how we use composite key in hibernate
need good example of hibernate with composit key
Hibernate: is it necessary to have primary key in table
Composite foreign key mapping in Hibernate 3 using JPA
Problem in Mapping Composite Primary Key with Hibernate