File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Primary key and update method strange issue 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 and update method strange issue" Watch "Primary key and update method strange issue" New topic
Author

Primary key and update method strange issue

Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
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??


SCJP, SCJD,SCWCD,SCDJWS,SCEA 5 MCP-C#, MCP-ASP.NET - http://www.khaledinho.com/
Life is the biggest school
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
there is no primary key.


42
Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Why do the create method throw a DuplicateKeyException???
Shan Jun Hao
Ranch Hand

Joined: May 23, 2006
Posts: 39
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.


SCJP, SCWCD, SCBCD, SCJD (In progress)
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
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 ]

SCJP 1.4,<br />SCJD (URLyBird 1.2.1, JDK 5.0)
Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64
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.


SCJP , SCJD. (IBM 142 in progress).
Maciej Miklas
Ranch Hand

Joined: Feb 12, 2007
Posts: 61
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

Joined: Feb 12, 2007
Posts: 61
sorry - might be wrong. The method create throws DuplicateKeyException....
Rudolph Jen
Ranch Hand

Joined: Nov 17, 2006
Posts: 37
>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


SCJP<br />SCJD (in progress)
Maciej Miklas
Ranch Hand

Joined: Feb 12, 2007
Posts: 61
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
Lucy Hummel
Ranch Hand

Joined: Apr 07, 2005
Posts: 232
Hi,

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

Regards, Lucy


----------------------------------<br />| SCJP, SCWCD, SCBCD, SCEA, SCJD |<br />----------------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primary key and update method strange issue