dougrand wrote:
Generating keys is a problem that isn't well addressed by any technology I'm aware of. The databases can support this to some extent (not really for composite keys), but generally you can't obtain the generated ids via JDBC.
There are a few approaches, but generally I've had a separate table with one or more rows, each of which generates an id for a separate column. For a composite key, normally all but one of the keys is a foreign key relationship and determined by the parent table.
It is easier and better in Hibernate to not use composite keys, but of course existing schemata require them. So you can either generate a unique key for the part of the composite that isn't a fk, or you can manage it via the object behaviors, i.e. check the other objects in the one to many collection and figure out the next id to put in the composite.
Exactly.
I've done this by creating a table w/ 3 columns. The first two are 64-bit integers (the min and max that a 64-bit int allows) and the third column is the sum of the first two columns - stored in hex format.
Each time a new key is "pulled", the first two columns are incremented by one, and a new value is stored in the third column.
It's a lot more cumbersome than just *not* using composite keys, as you said, but works well and can be easily represented as an entity bean.