• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Primary key and update method strange issue

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am working on the "Bodgitt and Scraper" assignment and i have the following wonders

According to the assignment specification the create method have the following declaration:

public int create(String[] data) throws DuplicateKeyException

I was thinking about what feilds in the table that make up the primary key.
Is it the contractor name??Is it the contractor name and the location together or other combination of feilds??

Another interesting thing which is:
Whatever combination of feilds the primary key was,the question is:Why dont the update method throw a DuplicateKeyException.The update method takes an array of strings as the new value for the feilds.Thinking of the data access as a seperate module,it is possible that the update method will take parameters so that it would violate the primary key constraint.Note that the
update method has void as a return type so there is no way for the client to know if the update operation succeeded or not.

What do you think about this issue and about the solution??
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no primary key.
 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do the create method throw a DuplicateKeyException???
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

there is no way for the client to know if the update operation succeeded or not.



Are you required to implement this method? Maybe you can take a look at the instructions again.

Why do the create method throw a DuplicateKeyException???



When a duplicate key is found existing in the database.

I think its more of how you eventually decide to implement the primary key.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Khaled,

I personally don't think you can create a personal key from the database Sun has provided, which I believe has been done on purpose to make us think and document our decisions accordingly - I would recommend searching the Javaranch SCJD archives for "primary key" - I read many posts on this topic and with the knowledge gained looked at my spec again before making this decision (however, you may decide you need a primary key for your spec)

Of course throwing a DuplicateKeyException depends on whether you actually use a primary key, but again I would recommend searching the archives for this topic.

Whatever combination of feilds the primary key was,the question is:Why dont the update method throw a DuplicateKeyException.




Again this depends on whether you use a primary key - if you do you will probably want to prevent a client from changing data within a field used in the primary key in a pre-existing record. If you think about it, it doesn't make sense to alter or "update" the data which is being used as a primary key or you will in essence be creating a new record (i.e. new primary key = new record). If the user does want to alter the primary key, I believe they should be forced to delete the record and create a new one. If you follow this advice, you will never have to throw a DuplicateKeyException or return any value from the update method (as the recNo will always remain the same).

However, as I've said before, this is just my take on the problem, and searching through the Ranch archives may lead you to a different conclusion, but I'm always happy to discuss...

Best wishes,

Daniel
[ August 12, 2006: Message edited by: Daniel Bryant ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can assume that combination of fields are unique(composite primarykey).

Since we have to strictly implement the interface, checking for the duplicates during update can be done in the client side.

Thanks.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got also B&S. You do not need primary key. Create method, creates simply a new record, without checking for duplicates. No required � not done. In the worst case your search will return two the same records.

Regards,
Maciej
 
Maciej Miklas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry - might be wrong. The method create throws DuplicateKeyException....
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>sorry - might be wrong. The method create throws DuplicateKeyException....

I agree here! I admit that the javadoc for the provided interface is a little bit unspecified about that Exception, but the name is clear.

Actually it isn't that hard:
1. Search for a record matching the fields of the record you would like to add
2. If you get some => throw new DuplicateKeyException()

Regards,
R
 
Maciej Miklas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rudolph Jen:
>sorry - might be wrong. The method create throws DuplicateKeyException....

I agree here! I admit that the javadoc for the provided interface is a little bit unspecified about that Exception, but the name is clear.

Actually it isn't that hard:
1. Search for a record matching the fields of the record you would like to add
2. If you get some => throw new DuplicateKeyException()

Regards,
R



This is really good idea � I will folow that
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What Rudolph suggest is that what I would do and assume as a client.

Regards, Lucy
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic