-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Surrogate key advocacy
PostPosted: Thu Oct 09, 2003 8:14 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Howdy folks,

Our DBAs are getting a bit shirty about our Hibernate oriented schema design.
The current issue is surrogate keys.

Frankly, surrogate keys these days are an automatic design decision for me because I've been burned too many times when "things change". Even if I was writing the code by hand I'd still be asking for them.

What I need now is the opinions of other people. Because of course, the opinions of the people hired to do the job based on skills and experience is not sufficient.

Does anyone know of any impressive "definitive source" for the pros and cons of surrogate keys?

Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 8:28 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Oh, also:

Where's a good place in the Hibernate doco that says "we'd really rather you used surrogate keys"?

I can't find anything definitive, but I'm pretty sure Hibernate prefers the use of surrogate keys, yes? I need reasons why.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 8:50 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no universal law, only best practice.

Don't use keys that change (business does) and if you can't find candidates, use surrogate keys. If you need composite keys for a unique natural key, consider a surrogate key instead. Beware: some natural keys look unique at first, but are not. This is where your DBA comes in, he should have the experience to find a good key for your data.

Hibernate works with surrogate, natural or composite keys just fine. I always use surrogate keys if I start with a new database. I don't see a reason to look for candidate keys first and I never had problems with surrogate keys. On the other hand, I got burned once with a natural key. Some people might argue that, if you can't find a natural unique key, your entity might miss some attributes. I can't see the point in this argument though.

A good summary: http://www.bcarter.com/intsurr1.htm

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 9:02 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Section 5.13 from the manual:
"If you've fully embraced our view that composite keys are a bad thing and that entities should have synthetic identifiers (surrogate keys),"

Natural keys mean to me personally that eventually, you will have composite keys. From my reading of the formus, I've gotten the impression that certain things won't work at their best when Hibernate is asked to use composite-keys.
Am I wrong?
Is there no real argument for using surrogate keys with Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 9:28 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Composite keys complicate your schema. This is not only true for ad-hoc SQL queries, DDL generation, etc, but of course also for the Hibernate mapping. As I said before: If you need a composite for a unique natural key, you should consider a surrogate key.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 9:34 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Ok, but other than increased complexity, there's no reason not use composite keys with Hibernate? That is, all features are supproted with composite keys?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 9:41 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Eh, that is a difficult one. :) I'd say that all important features are supported. It's not fun anymore, of course.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 9:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Natural keys are just evil.

Hibernate allows you to use them because most legacy database schemas have them somewhere.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.