如何使用 Nhibernate 删除多个数据库实体?

发布于 2024-08-14 04:24:56 字数 250 浏览 10 评论 0原文

解决这个问题的最佳实践是什么?是否有内置的批处理功能?

示例代码:

using (ITransaction transaction = _session.BeginTransaction())
{
   _session.Delete("FROM myObject o WHERE  o.Id = IN(1,2,...99999)");
   transaction.Commit();
}

提前致谢。

What is the best practice for this problem? Is there any batching features built-in?

Sample code:

using (ITransaction transaction = _session.BeginTransaction())
{
   _session.Delete("FROM myObject o WHERE  o.Id = IN(1,2,...99999)");
   transaction.Commit();
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-08-21 04:24:56

HQL支持IN子句,如果使用setParameterList,甚至可以传入一个集合。

var idList = new List<int>() { 5,3,6,7 };

_session.CreateQuery("DELETE MyDataClass o WHERE o.Id IN (:idList)")
    .SetParameterList("idList", idList)
    .ExecuteUpdate();

请注意,正如 ddango 在评论中提到的那样,对象中指定的关系级联将不会被执行,因为运行 HQL 查询只是转换为数据库查询,并且实际上不会加载任何实体对象。

HQL supports the IN clause, and if you use setParameterList you can even pass in a collection.

var idList = new List<int>() { 5,3,6,7 };

_session.CreateQuery("DELETE MyDataClass o WHERE o.Id IN (:idList)")
    .SetParameterList("idList", idList)
    .ExecuteUpdate();

Be aware, like mentioned by ddango in a comment, that relationship cascades specified in your objects will not be executed since running an HQL query simply translates to a DB query and does not actually load any entity objects.

醉梦枕江山 2024-08-21 04:24:56

我在获取答案时遇到问题,我发现以下查询有效 100%

        Session.CreateQuery("delete Customer c where c.id in (:deleteIds)")
            .SetParameterList("deleteIds", deleteIds)
            .ExecuteUpdate();

客户是类名而不是表名。
id 是小写的,在 HQL 中它是主键而不是类中的属性名称(支持属性名称)

I had problems getting the answer to work and I found the following query worked 100%

        Session.CreateQuery("delete Customer c where c.id in (:deleteIds)")
            .SetParameterList("deleteIds", deleteIds)
            .ExecuteUpdate();

Customer is the class name not the table name.
id is lowercase and in HQL it is the Primary Key not a property name in the class (Property names are supported)

烟雨凡馨 2024-08-21 04:24:56

您可以使用 HQL 删除多个对象

在这里查找删除- 对于 session.delete 示例

HQL DELETE 示例(您可以将 IN 与 HQL 一起使用):

ISession session = sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();

String hqlDelete = "delete Customer c where c.name = :oldName";
// or String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = session.CreateQuery( hqlDelete )
        .SetString( "oldName", oldName )
        .ExecuteUpdate();
tx.Commit();
session.Close();

you can Use HQL to delete multiple objects

Look for delete here - for session.delete example

HQL DELETE example (you can use IN with HQL):

ISession session = sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();

String hqlDelete = "delete Customer c where c.name = :oldName";
// or String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = session.CreateQuery( hqlDelete )
        .SetString( "oldName", oldName )
        .ExecuteUpdate();
tx.Commit();
session.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文