aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes primary key for a record? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "primary key for a record?" Watch "primary key for a record?" New topic
Author

primary key for a record?

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Hi,

having been assigned URLyBird 1.1.2, I couldn't identify any natural criteria to distinguish between different records.
Although, there's no such two records in the provided .db-file, for me there could potentially be two records specifying the same hotel in the same location with each a room for 4 persons, non-smoking, same price and available on the same date. Having this, there's no way now to discriminate the two records, so if one client requests to delete one of these records, which one is actually chosen?
Having no information about how to identify a record uniquely, I just created an artificial primary key by using the unique position of a record in the file.
This works well in my application, but anyway, is this the right approach expected by Sun?

Using the position of the record in the file as primary key now involves another problem:
The method specification of create() in interface DB declares a DuplicateKeyException in its throws clause.
Using the position of the file as primary key, however, makes throwing this exception actually obsolete, so it would never be thrown by my implementation of this method. On the other hand, if I had chosen the combination of all known fields in a record to be the the primary key, throwing a DuplicateKeyException would guarantee that no duplicates would ever happen to exist in the file.

Does this dilemma imply that I was wrong with my assumtion to have duplicates in the file?

Thanks in advance for your help,
Andy


SCJP, SCJD
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
having been assigned URLyBird 1.1.2, I couldn't identify any natural criteria to distinguish between different records.


My approach was the following:

There is one id... the position in the record file. Different line = Different entry. When there are two identical entries in the record file, then those are still different. There is nothing in my assignment forcing me to create an ID or to prevent identical entries. So recNo = lineNr.

The method specification of create() in interface DB declares a DuplicateKeyException in its throws clause.


Yuo donĀ“t have to use this exception. I completely ignored it.


Visit me at http://liferay-blogging.blogspot.com
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5277
    
  13

Hi Andy,

Having no information about how to identify a record uniquely, I just created an artificial primary key by using the unique position of a record in the file.
Me too

The method specification of create() in interface DB declares a DuplicateKeyException in its throws clause.
A throws clause doesn't mean this method must throw that kind of exception. It just means it might throw it. In the URLyBird application it is unlikely that this exception is thrown, because a real primary key (like the room number) is missing and a hotel can perfectly have two (or even more) different rooms with exactly the same characteristics (smoking not allowed, 2 persons,...)

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primary key for a record?