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

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

问题描述

考虑到以下代码块,为什么调用HQL工作但是调用delete()不起作用?作为背景,我使用NHibernate在IBM.Data.DB2.Iseries驱动程序。来了解一下,AS400上的日志记录被关闭,所以我不能使用交易。我不是AS400管理员或知道任何事情,所以我不知道关闭日志(不打开交易)是否导致这个问题。如果我正在调用Delete()或其他NHibernate函数,我是否需要打开交易?

  //这样做不工作 - 没有错误,没有删除
public static void Delete(Object Entity)
{
using(ISession session = _sessionFactory.OpenSession())
{
/ / using(ITransaction tx = session.BeginTransaction())
// {
session.Delete(Entity);
//tx.Commit();
session.Close();
//}
}
}

//这样做工作
public static void Delete(Object Entity)
{
使用(ISession session = _sessionFactory.OpenSession())
{
//注释掉事务控制,因为抛出错误,因为
//日志记录未打开AS400
//使用(ITransaction tx = session.BeginTransaction())
// {
session.CreateQuery(删除MyDAO p其中p.MyDAOID =:MyDAOID)。SetString(MyDAOID,((MyDAO)实体).MyDAOID.ToString())。ExecuteUpdate();
//}
}
}


解决方案

删除后,但在关闭会话之前尝试调用 session.Flush()。而且您不需要明确地关闭会话:

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


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();                
            //}
        }
    }

解决方案

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天全站免登陆