Am not sure my title is explicit enough. here is what i mean.I’m developing a web application using spring 3.0.5 + hibernate 3.6.1.RELEASE with JPA annotation using sessionFactory.The application allows users to signup using username, email and password.
users can log in using (username or email) and password. I’m trying to find a reliable way to tell there is a unique contraint exception expressed as
java.sql.BatchUpdateException: Duplicate entry 'myusername' for key 'USERNAME'ororg.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
if i could get a specific exception i could notify the user that his username or email is already taken.
What comes to my mind is to do a select before to check but then that doesn’t sound right to me.Any other solution ? what’s the best practice?
Thanks for reading this
Actually best practice would be to check if a user with that username/email address already exists when creating a new user. If it does then return something to indicate a failure, otherwise create the user.
Generally you shouldn’t rely on database constraints for enforcing business rules.