[Hibernate] Weak entities
- From: Florent Robineau <frobineau@xxxxxxxxx>
- Date: Sat, 17 Mar 2007 00:12:05 -0700
Hi everyone,
I've been working for a while with Hibernate now. However, still being
in the academics and hence imbued with the relational algebra and
normalization theory, the following statement that I found in the book
"Hibernate in Action" aroused my curiosity and bewilderment:
"Many legacy SQL data models use natural primary keys. [...] We strongly
recommend that new applications use synthetic identifiers (also called
surrogate keys). Surrogate keys have no business meaning - they are
unique values generated by the database or application."
Suppose for the sake of example that I want to model a library with its
book and their full content. I could set up a simplified (and somehow
inexact) model like this (I spare the details of the entities):
Word is a weak entity of Line, which is a weak entity of Page, which is
a weak entity of Book.
Now following the relational model logic, I would use as primary keys:
- for book, (book_id)
- for page, (book_id, page_number)
- for line, (book_id, page_number, line_number)
- for word, (book_id, page_number, line_number, word_index)
Using the other approach, I would denormalize it and it would look like
something like that:
- for book, (book_id)
- for page, (page_id) + unique condition on (book_id, page_number)
- for line, (line_id) + unique condition on (page_id, line_number)
- for word, (word_id) + unique condition on (line_id, word_index)
The second is easier for Hibernate and makes smaller tables, but is less
efficient if I want to perform queries like "Retrieve all the distinct
words of a given book sorted by frequency of apparition" since it would
require 3 joins whereas the first does not require any join at all.
What do you think about it? In what kind of situation should I use each
implementation (if ever)?
Thanks,
--
Florent
.
- Prev by Date: Re: Two of three SQL stmts execute: third results in table does not exist exception
- Next by Date: Re: FilteredRowSet size
- Previous by thread: reconnect, but no insert
- Next by thread: how to convert JAVA string in "hh:mm dd/mm/yyyy" to sql datetime format?
- Index(es):
Relevant Pages
|
|