• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

update() method does not throw duplicate key

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mohammad abdellatif,
Your post was moved to a new topic.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I will go to the second one
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic