Now, I'm trying to retrieve the record from the table.
Remember that the record could not be deleted by the delete statements, and it thus still exists in the table.
So, I add to the integration test a selectById statement.
The test now looks like:
@Test
public void testSave() {
Preference preference = new Preference();
preference.setName("A_UNIQUE_NAME");
preference.setValue("VALUE");
Integer preferenceId = preferenceDao.insert(preference);
assertNotNull(preferenceId);
preference.setName("A_UNIQUE_NEW_NAME");
preferenceDao.update(preference);
Preference loadedPreference = preferenceDao.selectById(preferenceId);
assertNotNull(loadedPreference.getId());
assertEquals(preference.getName(), loadedPreference.getName());
preferenceDao.delete(preference);
loadedPreference = preferenceDao.selectById(preferenceId);
}
But when running the test I got a
org.hibernate.ObjectNotFoundException: No row with the given identifier exists
Here is the stack:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.043 sec <<< FAILURE!
testSave(com.thalasoft.learnintouch.core.dao.PreferenceDaoTest) Time elapsed: 0.038 sec <<< ERROR!
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [com.thalasoft.learnintouch.core.domain.Preference#114]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.thalasoft.learnintouch.core.domain.Preference#114]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.load(HibernateTemplate.java:563)
at org.springframework.orm.hibernate3.HibernateTemplate.load(HibernateTemplate.java:557)
at com.thalasoft.learnintouch.core.dao.hibernate.PreferenceHibernateDao.selectById(PreferenceHibernateDao.java:40)
So I tried to catch the exception in the Dao class, by adding a catch block like:
Code:
public Preference selectById(final Integer id) {
Preference preference = null;
try {
preference = (Preference) getHibernateTemplate().load(PREFERENCE_CLASS, id);
} catch (ObjectNotFoundException e) {
}
return (preference);
}
But when running the test, I still got a org.hibernate.ObjectNotFoundException: No row with the given identifier exists.
Here is the stack:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.052 sec <<< FAILURE!
testSave(com.thalasoft.learnintouch.core.dao.PreferenceDaoTest) Time elapsed: 0.047 sec <<< ERROR!
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [com.thalasoft.learnintouch.core.domain.Preference#113]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.toto.core.domain.Preference#113]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.load(HibernateTemplate.java:563)
at org.springframework.orm.hibernate3.HibernateTemplate.load(HibernateTemplate.java:557)
at com.toto.core.dao.hibernate.PreferenceHibernateDao.selectById(PreferenceHibernateDao.java:40)
It's like the catch block did not catch the exception.