• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

create method and DuplicateKeyException  RSS feed

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My create method does not throw a DuplicateKeyException as it assigns new record number inside the create method and it does not enforce any primary key on the records fields. I know alot of people have taken this approach and just left the method signature as is and documented in their choices.txt.

For my update/delete and unlock methods i have overriden the signatures that were supplied in the DB interface due to issues i see as violations in how the API is used. So for example i have removed the RecordNotFoundException from the unlock method as it is possible that a thread who has the lock on a record may delete it.

Is there a case to do the same for the create method and override its signature so that it does not throw the DuplicateKeyException? The only issue i can see with this is that maybe in the future if the underlying database is changed then this method may need to throw that exception. Any thoughts? Thanks
 
Sheriff
Posts: 11604
178
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Colin Duggan wrote:The only issue i can see with this is that maybe in the future if the underlying database is changed then this method may need to throw that exception. Any thoughts?


And that's exactly the reason why I would not override this method. With update/delete/unlock methods it makes sense, because this exception will never be thrown from these methods. That's not guaranteed with the create-method. Just my 2 cents.
 
Colin Duggan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I just registered for the exam last week, and did a little bit of work on past weekends. I was reading this forum to see how people solved their problem and I'm not quite sure why you shouldn't throw DuplicateKeyException? I wrote a unit test where I'm trying to add two valid records which have same name and specialties and test should expect DuplicateKeyException be thrown. If I understand task correct, we should enforce entity integrity by checking, if combination of fields which creates primary key already exist in database. So we have to prevent adding records with same primary keys. It's not a rocket science to do, and it makes perfect sense for me. Am I missing something?

 
Roel De Nijs
Sheriff
Posts: 11604
178
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roman Burdakov wrote:Am I missing something?


No When you have the contractors assignment it makes sense to throw a DuplicateKeyException, when you have the hotel rooms assignment it makes no sense at all.
 
Roman Burdakov
Greenhorn
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Thanks for the clarification.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!