Hibernate删除无法正常工作

时间:2013-02-21 13:56:43

标签: hibernate delete-row

在调试我的服务器端代码时,我意识到我的删除行无效。我刚从插入中复制了此代码,并将session.saveORUpdate替换为session.delete。这是删除对象的正确方法吗?我在这里做错了什么?

我没有任何例外。

public void delete(Object dataStore) throws DidNotSaveRequestSomeRandomError {
    Transaction txD;
    Session session;
    session = currentSession();

    //get first and then fallback to begin.
    if (session.getTransaction() != null
            && session.getTransaction().isActive()) {
        txD = session.getTransaction();
    } else {
        txD = session.beginTransaction();
    }

    try {
        session.delete(dataStore);
    } catch (MappingException e) {
        e.printStackTrace() ;
    }
    try {
        txD.commit();
        while (!txD.wasCommitted())
            ;
    } catch (ConstraintViolationException e) {
        log.error("Unable to delete data from "
                + dataStore.getClass().toString());
        txD.rollback();
        throw new DidNotSaveRequestSomeRandomError(dataStore,
                feedbackManager);
    } catch (TransactionException e) {
        log.debug("txD state isActive" + txD.isActive()
                + " txD is participating" + txD.isParticipating());
        log.debug(e);
        txD.rollback();
    } finally {
        session.flush();
        txD = null;
        session.close();
    }
}

编辑:

我尝试在session.delete之后放置session.flush,但没有用。

0 个答案:

没有答案