NHibernate - 为什么 Delete() 调用无法删除但通过 HQL 删除有效? [英] NHibernate - Why does Delete() call fail to delete but delete through HQL works?

查看:16
本文介绍了NHibernate - 为什么 Delete() 调用无法删除但通过 HQL 删除有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到以下代码块,为什么调用 HQL 有效但调用 delete() 无效?作为背景,我在 IBM.Data.DB2.Iseries 驱动程序上使用 NHibernate.来了解一下,AS400 上的日志功能已关闭,因此我无法使用事务.我不是 AS400 管理员或对此一无所知,所以我不知道关闭日志(不打开交易)是否会导致此问题.如果我调用 Delete() 或其他 NHibernate 函数,我是否绝对需要打开事务的能力?

Considering the following code blocks, why does call to HQL work but call to delete() not work? As a background, I'm using NHibernate over IBM.Data.DB2.Iseries driver. Come to find out, journaling on the AS400 is turned off so I can't use transactions. I'm not the AS400 admin or know anything about it so I don't know if having journaling turned off (not opening transactions) is causing this problem or not. Do I absolutely need the ability to open transactions if I'm calling Delete() or other NHibernate functions?

//This Does not work - no error and no deletes
public static void Delete(Object Entity)
    {
        using (ISession session = _sessionFactory.OpenSession())
        {
            //using(ITransaction tx = session.BeginTransaction())
            //{
                session.Delete(Entity);
                //tx.Commit();                    
                session.Close();                    
            //}
        }
    }

//This does work
public static void Delete(Object Entity)
    {
        using (ISession session = _sessionFactory.OpenSession())
        {
            //commented out transaction control because throws error because
            //journaling is not turned on the AS400
            //using(ITransaction tx = session.BeginTransaction())
            //{
                session.CreateQuery("delete MyDAO p where p.MyDAOID = :MyDAOID").SetString("MyDAOID", ((MyDAO)Entity).MyDAOID.ToString()).ExecuteUpdate();                
            //}
        }
    }

推荐答案

尝试在删除之后、关闭会话之前调用 session.Flush().而且您不需要显式关闭会话:

Try calling session.Flush() after you delete, but before you close the session. And you don't need to explicitly close the session:

using (var session = ...)
{
    entity.Delete();
    session.Flush();
}

这篇关于NHibernate - 为什么 Delete() 调用无法删除但通过 HQL 删除有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆