Hibernate Exception – Could not synchronize database with session

Posted on July 24, 2010

0


One fine saturday morning, trying to do a new release. A list all action that always worked started throwing up exceptions in Hibernate.

Could not synchronize database with session

Duplicate entry ‘1’ for key 2

I wasn’t even inserting or updating … and yet this suddenly broke.

I figured out after a few frustrating hours one of the fields (mobile number) had a unique constraint … and I had recently modified the property setter to only update with valid values. There were some old records in the database (a.ka. testing) where the mobile number was invalid and hence it was no longer able to load the value into the java object … hence the database and session objects mis-match.

Hibernate errors are confusing as ever.

Could not synchronize database with session

If you face this error … read it as … there are some property values that are not matching with the database … in either direction …

Duplicate entry ‘1’ for key 2

If you face this error … read it as … there is 1 record that is violating 2nd key constraint ( unique constraint)

Advertisements
Posted in: Hibernate