Jon said:
Yes - you shouldn't be doing that. The point of an identity is that
it's a way of identifying something - identities shouldn't change. If
you need to be able to update a piece of data, that data shouldn't be
part of the identity.
My guess is that you're using a natural key (one with business
meaning) as the primary key. From the Hibernate in Action book:
<quote>
Experience has shown that natural keys almost always cause problems in
the long run. A good primary key must be unique, constant, and
required (never null or unknown).
</quote>
Note the "constant" part.
With natural keys you have the problem that if the user makes a typo,
and wants to correct it, the user can't because the object is already
saved. Which is unfortunate and silly, from the user's POV, because the
field is just part of the entity s/he's editing.
Updating PK's isn't that hard for an O/R mapper, after all the
original PK value is known, so the O/R mapper can do: if the entity
isn't new and the pk field is changed (e.g.: different from its
original value), generate an update query where you use the original
value as the PK filter, setting the pk to the new value.
The problem with this is that it has to be a single-entity operation
and the developer has to make sure fk's are updated first (or use
cascading updates inside the db, as it's never known which objects are
related unless all objects are loaded from the db), which can be a
tedious operation. THe developer has to do that manually, because the
O/R mapper can't assume it's to be done automatically, simply because
if the FK changes, the row changes and that can have severe
consequences for the overall system, which therefore shouldn't be
automatic, hence the manual operation.
But besides that, it's a few lines of code inside an O/R mapper core.
FB
--