No�l
My thinking is to take a record number as a primary key. Is that valid?
SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Originally posted by No�l Verdurmen:
In my assignment the create method throws a DuplicateKeyException. However, I find it hard to say what the primary key is.
My thinking is to take a record number as a primary key. Is that valid?
Choosing the name and/or location as primary key seems inappropriate to me. Isn't it possible that there are more free rooms in the same hotel?
SCJP 1.4, SCJD
Record number is never a valid primary key. Record primary key must be unique, and invariant.
No�l
Originally posted by Hanna Habashy:
[QB]Noel,
Record number is never a valid primary key. Record primary key must be unique, and invariant. I mean by that the record primary key must not change because the record position changes for any reasons such as ordering the database records.
The uniqueness comes from a unique data elements or peoperites about this specific entity. Properties that no other enitiy could have.
There is must be something uniquely identify your individual record, you have to find it..
SCJP 1.4, SCJD
And if you are not familiar with UyB, then there very arguably is not.
SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
No�l
But what still wonders me is why that DuplicateKeyException is in the specified create() method? There must surely be an elegant solution?
SCJP 1.4, SCJD
Anton Golovin ([email protected]) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
There is no requirement for a primary key at all.
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Originally posted by Anton Golovin:
Hi, Mike! I throw the DuplicateKeyException. I do in for the physical record number (which is also the logical record number in my cache.) If I am creating a record, I find out a "deleted" record (I have a TreeSet that keeps the numbers), check if it exists in the cache, and if it does, I throw a DuplicateKeyException. If the deleted list is empty, I figure out the record number to assign to the new record, then check it against the keys in my HashMap (the cache), and if a duplicate exists, I throw the exception. Maybe it is not what they are looking for, but I need to throw something there. Of course, I will document the fact that it should never really be thrown in the control flow. The problem for me was that I use io facilities in the method as well (to write reused record to disk before committing it to the cache.) create method must return an integer, and can only throw DuplicateKeyException - not by any means a good wrapper for an IOException. So my method returns -1 for any error conditions.
[ August 23, 2004: Message edited by: Anton Golovin ]
Originally posted by peter wooster:
Just because the interface provides for the throwing of an exception, there is no need to actually throw it. Here you are confusing "necessary" and "sufficient". Just because the interface provides the DKE doesn't mean it requires it. Inconsistencies between the actual data and your cache are not a good use of DKE. To get a DKE you must have a real primary key. In the UrlyBird there is no ability to provide a primary key. The obvious PK is Name,Location,Room,Date. Since Room is not in the database there is no ability to define a primary key, so no DKE.
[ August 23, 2004: Message edited by: peter wooster ]
Anton Golovin ([email protected]) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
I am probably confusing things. I am throwing the DuplicateKeyException because the primary key in my cache is the record number. I can prove it: each record number is unique. Maybe I misunderstand something. If it can't be done, then I won't do it - but so far, it looks like there is a primary key which should not be duplicated when a new record is created.
SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Mike:
An elegant solution is recognising that there is no primary key, and documenting that fact and that DKE will never be thrown. There is no requirement that an interfaces Exceptions should be thrown.