如何一次性使用NHibernate运行多个插入? [英] How can I run multiple inserts with NHibernate in one go?

查看:92
本文介绍了如何一次性使用NHibernate运行多个插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到使用NHibernate运行多个(> 1000个)插入语句的最有效方法.

I am trying to find the most efficient way of running multiple (> 1000) insert statements with NHibernate.

实际的插入语句非常简单,因为它使用新创建的对象中的FK ID和子查询中的值.这是我要用SQL编写的内容:

The actual insert statement is very simple as it uses an FK ID from a newly created object together with values from a subquery. Here's what I would write in SQL:

insert into dbo.NotificationView
select 
    1 AS IDOfNewItem, 
    U.Id AS UserID, 
    0 AS HasEdited
from
    [User] U
    INNER JOIN UserSite US ON U.Id = US.UserId
where
    US.SiteId = 1

我已经看到有一个名为adonet.batch_size的参数( NHibernate:插入多个可以同时在休眠配置" XML文件中设置这些项目),但是看来这只会创建与对象相同数量的insert语句.

I have seen that there is the parameter called adonet.batch_size (NHibernate: insert multiple items at once) that can be set in the "hibernate-configuration" XML file, but it appears that this will simply create the same number of insert statements as there are objects.

有没有一种方法可以一次执行插入操作,而无需遍历每个项目?

Is there a way to run the insert in one go, without iterating through each item?

如果是这样,这是否会对缓存产生负面影响?

If so, does this negatively affect the cache in any way?

推荐答案

adonet.batch_size有效,只要:

  • 您正在使用受支持的数据库(SQL Server,不确定Oracle)
  • 您使用的是受支持的生成器(例如,身份无效)
  • 您对实体不做任何奇怪"的事情.

无论如何,看来您正在尝试进行完全不相关的批量插入.

Anyway, it looks like you are trying to do a bulk-insert, which is totally unrelated.

您可以在 DML样式的操作中完成此操作HQL.

You can accomplish that with DML-style operations in HQL.

这篇关于如何一次性使用NHibernate运行多个插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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