NHibernate - 为什么Delete()调用无法删除,但是通过HQL进行删除? [英] NHibernate - Why does Delete() call fail to delete but delete through HQL works?
问题描述
//这样做不工作 - 没有错误,没有删除
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屋!