This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • 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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic