How can I run multiple inserts with NHibernate in one go?

纵饮孤独 提交于 2020-01-17 06:00:12

问题


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

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

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?


回答1:


adonet.batch_size works, as long as:

  • You are using a supported DB (SQL Server, not sure about Oracle)
  • You are using a supported generator (for example, identity doesn't work)
  • You don't do anything "strange" with your entities.

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

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



来源:https://stackoverflow.com/questions/4166590/how-can-i-run-multiple-inserts-with-nhibernate-in-one-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!