Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Error when someone tries to register with duplicate userId

 
Ranch Hand
Posts: 91
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While registering a user, I want to check that if the userId is unique, then only it registers, otherwise it should give an error message, I have written the following code, to check if user exists, but it is still registering a new user with duplicate userId, what am I missing? Thanks.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the code can save a duplicate userId, it seems your database table is missing a primary key constraint or unique constraint on the userId column.

And did you already debug your code to see what happens when a duplicate user is registered? When an exception occurs (e.g. a constraint violation exception) in the register() method, you would expect false as return value but you are currently returning true.
 
arushi tomar
Ranch Hand
Posts: 91
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Resolved.

I put constraint manually in the table, and did this:



and gave pop ups for both fail and success.
Thanks.
 
arushi tomar
Ranch Hand
Posts: 91
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:If the code can save a duplicate userId, it seems your database table is missing a primary key constraint or unique constraint on the userId column.

And did you already debug your code to see what happens when a duplicate user is registered? When an exception occurs (e.g. a constraint violation exception) in the register() method, you would expect false as return value but you are currently returning true.



Thank you
 
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

arushi tomar wrote:Resolved.

I put constraint manually in the table, and did this:


Still confused why the code to create a new user is executed while the isUserExists() method is returning true
 
arushi tomar
Ranch Hand
Posts: 91
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

arushi tomar wrote:Resolved.

I put constraint manually in the table, and did this:


Still confused why the code to create a new user is executed while the isUserExists() method is returning true



Yeah, even I am, that shouldn't happen. :-/
 
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

arushi tomar wrote:Yeah, even I am, that shouldn't happen. :-/


That brings us back to my original question: did you already debug your code to see what happens when a duplicate user is registered? Does the isUserExists() method returns true (as you would expect)? Or maybe it returns false for some reason?
 
arushi tomar
Ranch Hand
Posts: 91
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

arushi tomar wrote:Yeah, even I am, that shouldn't happen. :-/


That brings us back to my original question: did you already debug your code to see what happens when a duplicate user is registered? Does the isUserExists() method returns true (as you would expect)? Or maybe it returns false for some reason?



It returned true but still entered the part of saving user, I resolved it before I could check the problem in deep though. I will check it once again after I am done with the other work, and update the solution here.
Thanks :-)
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting .... Please let us know, whey it was going to save even if isUserExists() method returns true.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you have to handle the situation where a userId exists during the insert operation anyway, there's little point in the isUserExist method.
I'd just get rid of it and let the db tell you of the ConstraintViolation (which you're handling) when you try the insert.
 
straws are for suckers. tiny ads are for attractive people.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic