csell (6) [Avatar] Offline
#1
Hello,

on page 51ff of the most recent MEAP edition you mention that primary keys must be immutable. That statement qualifies at best as your personal opinion, but it is not true as a general rule. The SQL standard does not impose such a requirement, neither does any of the RDBMS that I know of.

SQL even provides a "ON UPDATE CASCADE" clause to cater for primary keys that are pointed to by foreign keys, which again goes against the statement from page 52 "Changing the value (or even definition) of a primary key, and all foreign keys that refer to it, is a frustrating task". Frustrating for who?

regards,
Christian Sell
Christian Bauer (56) [Avatar] Offline
#2
Re: Immutable Primary Key?
Picking a candiate key as the primary key is purely a matter of taste. For Hibernate to work properly, a candidate key must be immutable if it's supposed to work as a primary key.
csell (6) [Avatar] Offline
#3
Re: Immutable Primary Key?
so the immutability requirement is imposed by hibernate and/or by JPA? That should be made more clear from the text IMO.

That would also imply that a legacy schema that relies on mutable keys cannot be mapped with Hibernate/JPA
Christian Bauer (56) [Avatar] Offline
#4
Re: Immutable Primary Key?
I have added a note:

The relational model defines that a candidate key must be unique and irreducible (no subset of
the key attributes has the uniqueness property). There are no nulls in the relational model, so
this requirement is related to SQL. Picking a candidate key as <em>the</em> primary key is
actually a matter of taste. However, Hibernate expects that a candidate key is also immutable
when it's used as the primary key. Hibernate doesn't support updating primary key values with an
API, and you'd run into problems with Hibernate's caching and dirty checking engine if you try
to work around this requirement. If your existing database schema relies on updatable primary
keys (and maybe uses <code>ON UPDATE CASCADE</code> foreign key constraints), you must
change the schema before it will work with Hibernate.
csell (6) [Avatar] Offline
#5
Re: Immutable Primary Key?
great. That covers it.. I assume that what you call irreducible is synonymous to a violation of NF2? IMO normal forms aren't a strict technical requirement, after all theres the common practice of denormalization..