I've seen the same exception, even after doing the 'session.get()' and checking for null (which it isn't.) What's even worse is that this is a generic base class (DAO) for deleting objects. One of my objects deletes just fine, but another produces the 'Could not synchronize database state with session'... HibernateException: SQL insert, update or deletefailed (row not found).
Any idea(s)?
Delete method:
Code:
public void delete(Object obj)
throws SQLException, InvalidOperationException, ObjectNotFoundException
{
try {
net.sf.hibernate.Session session = sessionFactory.openSession();
session.connection().setCatalog(companyName);
logger.debug("DELETE: connected to catalog " + session.connection().getCatalog());
Transaction tx = null;
Object found = null;
try {
if (null == (found = session.get(obj.getClass(), new Integer(((DBObject)obj).getID())))) {
throw new ObjectNotFoundException(getClass() + "#" + ((DBObject)obj).getID());
}
else {
tx = session.beginTransaction();
session.delete(found);
tx.commit();
}
}
catch (HibernateException e) {
logger.error("DELETE: Unhandled Exception: ", e);
if (tx != null) {
tx.rollback();
logger.error("DELETE: rolled back transaction: " + tx);
}
}
finally {
session.close();
}
}
catch (HibernateException e) {
logger.error("DELETE: HibernateException: ", e);
throw new InvalidOperationException(e);
}
}
And the mapping:
Code:
<class name="Contact" table="CONTACT">
<id name="ID" column="ID" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="address1" column="address1"/>
<property name="address2" column="address2"/>
<property name="city" column="city"/>
<property name="code" column="code"/>
<property name="companyName" column="companyName"/>
<property name="contactName" column="contactName"/>
<property name="country" column="country"/>
<property name="email1" column="email1"/>
<property name="email2" column="email2"/>
<property name="fax" column="fax"/>
<property name="name" column="name"/>
<property name="notes" column="notes"/>
<property name="phone1" column="phone1"/>
<property name="phone2" column="phone2"/>
<property name="state" column="state"/>
<property name="status" column="status"/>
<property name="url1" column="url1"/>
<property name="url2" column="url2"/>
<property name="zipCode" column="zipCode"/>
</class>
[/code]