wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes update() method does not throw duplicate key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "update() method does not throw duplicate key" Watch "update() method does not throw duplicate key" New topic
Author

update() method does not throw duplicate key

mohammad abdellatif
Greenhorn

Joined: Aug 29, 2007
Posts: 24
Hi everyone.

DBMain.update() method does not throw a duplicate key exception, so correct me if I am wrong, the assumption was to not update the primary keys fields inside this method, and the person who wants to update a record primary key fields, he has to delete the record then reinsert it again.

This means I have to handle duplication for keys inside the update method in one of the following scenarios:
1- Ignore primary key fields came in passed array fields and update the other fields.
2- Read the record data again inside update method, check if primary key fields passed are not changed, if is, throw an IllegalStateException to indicate the caller he can not update a record key, then update the records data accordingly in case the primary keys are not changed.

Of course extra JavaDoc will be added to update method to explain the expected scenario.

Please advice, which is the best way to handle it.
I am appreciate your fast response because I am planing to upload my assignment within few days
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

mohammad abdellatif,
Your post was moved to a new topic.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

In my assignment I didn't have a primary key in the database file (containing hotel rooms), so I had no need to handle this. But I think it's a bad idea to update a primary key.

Based on your 2nd scenario: how will you retrieve the correct record if primary key has changed, because you would expect that you retrieve records based on the primary key.
mohammad abdellatif
Greenhorn

Joined: Aug 29, 2007
Posts: 24
Maybe i was not clear enough, the signature of update method is like below (taken from assignment):

So I already have the record no to read the record from database according to it, then update its fields.

Is this what you was asking??

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

I think you can follow both approaches you described and just describe why you choose the one or the other. I think I prefer the 2nd one, because it protects your data by throwing an exception (even if your Data class is used from another user interface). This IllegalStateException should never be thrown in your application (because you should prevent these values from being changed)
mohammad abdellatif
Greenhorn

Joined: Aug 29, 2007
Posts: 24
I think I will go to the second one
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: update() method does not throw duplicate key
 
Similar Threads
NX: Home contractor assignment, update()/create(), DuplicateKeyException
B&S: Primary Key
Create method [B&S]
@deprecated DuplicateKeyException
Nx: Documenting DuplicateKeyException