-->
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.  [ 5 posts ] 
Author Message
 Post subject: The rules changed while I wan't looking?
PostPosted: Fri Oct 15, 2004 10:16 pm 
Newbie

Joined: Mon Oct 11, 2004 11:58 pm
Posts: 3
I'm very new to Hibernate. In fact I haven't even had a chance to try it yet. But I've been reading Hibernate in Action, and I've been lurking around the forums, and suddenly it seems that everything I thought I knew about database design has turned out to be wrong.

For instance, why are composite keys bad? Why does the book keep using the dirty word "legacy" when referring to them? Why is table-per-class-hierarchy recommended over table-per-subclass? Consider the following structure (only keys are shown):

Code:
create table Party (
  party_id int not null primary key
);

create table Person (
  party_id int not null primary key references Party(party_id)
);

create table Organization (
  party_id int not null primary key references Party(party_id)
);

create table RoleType (
  role_type_id int not null primary key
);

create table RelationshipType (
  from_role_type_id int not null references RoleType(role_type_id),
  to_role_type_id int not null references RoleType(role_type_id),
  primary key (from_role_type_id, to_role_type_id)
);

create table Relationship (
  from_party_id int not null references Party(party_id),
  to_party_id int not null references Party(party_id),
  from_role_type_id int not null,
  to_role_type_id int not null,
  primary key (from_party_id, to_party_id, from_role_type_id, to_role_type_id),
  constraint fk_Relationship_RelationshipType
    foreign key(from_role_type_id, to_role_type_id) references
    RelationshipType(from_role_type_id, to_role_type_id)
);


This ddl is a common idiom for a database that contains information about people and organizations. It is a data modeling pattern in the same sense that Facade & Flyweight are object modeling patterns. If I were to say "party structure" to an experienced data modeler, I would expect the image of a structure similar to the one above to appear in his/her head. Yet this structure makes heavy use of compound keys, and it implies a table per subclass class model.

I could certainly introduce new surrogate keys in place of the compound keys, but I would have to complicate the design with unique indexes to maintain data integrity. And I could flatten the Party-Person-Organization thing, but my boss would probably send me back to Database Normalization 101 (if I was lucky).

I like the whole idea of Hibernate and ORMs in general. And I understand that Hibernate can work with the structure above, and I can't wait to try it out. But I don't understand why the book seems to recommend compromising the data model to save the code.

Isn't data still much more valuable than code? Twenty years from now which is more likely to still have any value: your java program, or your data?


Top
 Profile  
 
 Post subject: Here's a picture
PostPosted: Sat Oct 16, 2004 12:02 am 
Newbie

Joined: Mon Oct 11, 2004 11:58 pm
Posts: 3
I ran the ddl above in PostgreSQL and reverse-engineered it with Visio. Here's the jpeg:
Image


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 3:43 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
For instance, why are composite keys bad? Why does the book keep using the dirty word "legacy" when referring to them? Why is table-per-class-hierarchy recommended over table-per-subclass?


I suggest you read again, because that is certainly not the case. Why are composite keys bad? Because they often imply a natural key as well.

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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 10:07 am 
Newbie

Joined: Mon Oct 11, 2004 11:58 pm
Posts: 3
Thanks for replying Christian.

So you'd agree that this structure is sound?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 10:50 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sure, as long as you don't trust unstable natural keys, composite keys only mean "more work" in certain situations. Why? Because you have to make sure that the correct keys are assigned yourself and can't use some automatic generator. As long as you are aware of this, there isn't a problem with composite keys from a modeling perspective.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.