0

I have created a table in database using hibernate and one of the attribute(username) is a primary key.So when i try to insert duplicate values in the username i'm getting the following exception.Now i have to handle this exception so as to prompt the user as "username already exists".

22462 [http-8080-2] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null 22462 [http-8080-2] ERROR org.hibernate.util.JDBCExceptionReporter - failed batch 22462 [http-8080-2] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session

  • Possible duplicate of -http://stackoverflow.com/questions/3820452/hibernate-could-not-synchronize-database-state-with-session – Rocky Feb 25 '12 at 05:08

1 Answers1

1

IMO, better approach would be to first make a get query i.e. find the object by PK and if returns a not null objects, you can show the error message of "duplicate entry" to the user otherwise insert the record in the database.

vikas
  • 1,535
  • 1
  • 13
  • 22