• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update() method does not throw duplicate key

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