【发布时间】:2018-12-31 20:42:54
【问题描述】:
我正在尝试使用休眠从数据库中删除对象 当我点击删除按钮时,休眠中出现错误
org.hibernate.HibernateException: delete is not valid without active transaction
这里是我删除时的代码
int id = eStoreUserDTO.geteStoreStoresDTO().getId();
StringBuilder hql = new StringBuilder();
hql.append("select distinct eStoreStoresDTO from EStoreStoresDTO"
+ " eStoreStoresDTO where eStoreStoresDTO.id = "+ id);
List<EStoreStoresDTO> store = DaoFactory.getDao().selectAllHql(hql + "");
eStoreUserDTOs.remove(eStoreUserDTO);
DaoFactory.getDao().delete(eStoreUserDTO);
EStoreStoresDTO dto = store.get(0);
DaoFactory.getDao().delete(dto);
删除方法:
@Override public void delete(Object object) throws DaoException {
Session session = null;
Transaction transaction = null;
try {
session = getCurrentSession();
transaction = session.getTransaction();
session.delete(object);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DaoException(e);
}
}
【问题讨论】:
-
1) 请复制您的代码,不要放图片。 2)你可以添加你的 DaoFactory.getDao() 的代码吗?谢谢
-
@Leviand ` @Override public void delete(Object object) throws DaoException { Session session = null;交易交易=空;尝试 { session = getCurrentSession();事务 = session.getTransaction();会话.删除(对象);事务.commit(); } catch (Exception e) { if (transaction != null) { transaction.rollback(); } 抛出新的 DaoException(e); } }`
-
@Leviand:抱歉,我拒绝了您的编辑(至少是这样投票的)。现在只看到您从评论中获取代码,在编辑的评论中错过了代码。不幸的是,不能“撤消”我的投票。对不起