• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primary key and Hibernate

 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that a table has to have a Primary Key if we want to map that through Hibernate ? Or is there any way to map a table without a PK ?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There just has to be a way to Uniquely identify a row in the table. So it could be a combination of fields that make up the PK. Or a combination of fields that makes the record/row unique.

Mark
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Somak Dalui:
Is it true that a table has to have a Primary Key if we want to map that through Hibernate ? Or is there any way to map a table without a PK ?


To add to what Mark has already written; to store relational data in a database (irrespective of whether you use Hibernate or not) you need a primary key for every entity you model. The issue being that unless you can uniquely identify a record it can't be part of a relationship, so its not relational. Its not a big issue adding a surrogate key to every table, so I'd suggest this is always the best way to go. Its also the route the Hibernate documentation recommends.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic