File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Primary Key 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 "B&S: Primary Key" Watch "B&S: Primary Key" New topic

B&S: Primary Key

Bhavik Patel
Ranch Hand

Joined: Jul 12, 2004
Posts: 155
In my assignment , the spec says clearly that if the method declares DuplicateKeyException to be thrown then it should do so if DuplicateKey found.

so i can not avoid this requirement and not throw DuplicateKeyException as i saw some people have just ignored this requirement in the past.

Now the question is what should be the primary key ..

recNo as primary key may create problem in case of create-delete sequence but locking should avoid this..

I have implemented name & location combination as composite key.
I am setting the primary key object with keys(int[] keys) as member variable which sets the index of array fields which are primary key elements ..
so in my case

In create method i check for duplicateEntry by comparing the name & location.and if its valid then just create new record,

in the update i am trying to check the new values will not violate the name& location(composite key) and if its all valid then & then update.

My concern is that if in any case primary key constraint is violated then the operationis failed throwing the DKE and message is logged .Does it fullfill the requirement ??any drawbacks of this decision ?? just wanted some views on this design ...


SCJP 1.4<br />SCWCD 1.4(91%)<br />Working on SCJD -Bodgitt & Scrapper Constructions...<br /> <br />"It takes 43 muscles to frown & 17 to smile but it doen't take any to just sit there with a dumb look on your face .. Keep Smiling "
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
Pretty much what I did.

I've also documented around referential integrity in the user documentation, explaining what conditions will cause the exception to occur.


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Yes, the requirements say to throw a duplicatekeyexception if there's an attempt to generate a duplicate key.
But as the requirements don't specify a key, there's no way (unless you, like you did, specify your own) to ever generate a duplicate key as there's no key at all

All depends on how you explain the requirements I guess, just make sure you (as always) document your decisions.

Bhavik Patel
Ranch Hand

Joined: Jul 12, 2004
Posts: 155
thanks for the information .. I will document my decisions about it.
I agree. Here's the link:
subject: B&S: Primary Key
It's not a secret anymore!