[Hibernate] Weak entities



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
.



Relevant Pages

  • Re: hibernate to legacy DB with no ID columns
    ... Hibernate supports custom types which are intended to be used in ... middlegen will look at the unique columns and primary keys from the ... table's underlying DDL. ...
    (comp.lang.java.programmer)
  • Re: Read Database schema with java
    ... Relations, Tablefield, Primary Keys, NULL/ NOT Null. ... did u know any consept. ... did u know a method with hibernate ...
    (comp.lang.java.databases)