• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Multiple Primary Key Mapping

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What's the best way to go about mapping multiple primary key columns in hibernate?

I've done some reading about composite id's but can't decide if this is what's required.

For instance, given the database table:

SupplierBankDetails accountNo, sort, supplierID)

where accountNo and sort are primary keys whilst supplierID is a forign key?

The table could be anything....the point is how would one map two columns (like accountNo and sort) as the id mapping value?

Any advice would be greatly appreciated,

Cheers!
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a point about terminology here: there is no such thing as more then one primary key. An entity has a primary key that uniquely identifies it; it can't have two unique identifiers. The primary key itself may be composite, in which case the key is composed of more than one attribute. If this is how you have defined your table then a composite key is the way to go.

However, looking at your data I think there is an example of a surrogate key in there. Account numbers are a classic example of surrogate keys (a value created that has no business meaning, so whose sole purpose is to uniquely identify a thing). I don't know the details of yourr data, but couldn't the account number be used as the primary key? I mention it because composite keys are always best avoided if possible.
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for clearing that up!

Yes...i'm sorry i meant to say two attributes that make up the primary key.

That example above was pretty bad that i thought of on the spot...yes in that example i guess accountNumber could and probably should be the primary key. However i often work on database tables (that already exist and difficult to change) that have multiple columns as the primary key...so i know now that composite mapping is whats required.

Thanks again
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic