我想刪除數據庫中不存在的實體,但delete()方法不會發出任何異常。
當我嘗試刪除不存在的實體時,如何得到錯誤?
我抄我下面的代碼:什麼時候org.hibernate.Session拋出HibernateException?
public void remove(MyEntity persistentInstance) {
logger.debug("removing entity: " + persistentInstance);
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
logger.debug("remove successful");
} catch (final RuntimeException re) {
logger.error("remove failed", re);
throw re;
}
}
編輯:
我呼籲在使用下面的代碼測試的刪除:
final MyEntity instance2 = new MyEntity (Utilities.maxid + 1); //non existent id
try {
mydao.remove(instance2);
sessionFactory.getCurrentSession().flush();
fail(removeFailed);
} catch (final RuntimeException ex) {
}
即使我把沖洗測試沒有按」不合格,爲什麼?
我想獲得例外。無論如何,我也有興趣瞭解delete()何時會拋出異常。