56758

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

Question:

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:

<pre class="lang-sql prettyprint-override">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 (<a href="https://stackoverflow.com/questions/2833406/nhibernate-insert-multiple-items-at-once" rel="nofollow">NHibernate: insert multiple items at once</a>) 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?

Answer1:

adonet.batch_size works, as long as:

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

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

You can accomplish that with <a href="http://nhforge.org/doc/nh/en/index.html#batch-direct" rel="nofollow">DML-style operations</a> in HQL.

Recommend

  • WCF net.tcp transport security - how to disable server certificate validation on the client
  • Mongo Index not being used
  • Can not find reason for crash test flight
  • How to change system locale in batch
  • Creating certificate using makecert without pvk file
  • OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to api.amazonalexa.com:443
  • iPhone — MKReverseGeocoder.adminstrativeArea — getting state abbreviation
  • CardLayout display Next panel - java Swing
  • Eclipse : Unknown Source Java SQL Exception
  • Why does _GET in PHP wrongly decodes slash?
  • cURL for Windows can't make a secure connection to the Stack API
  • Cleave.js Phone CA
  • SSLRead() return error -9806/15958)
  • What distributed message queues support millions of queues?
  • JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
  • How do you SELECT several columns with one distinct column
  • How to retrieve multiple columns from non-entity type sql query?
  • How to concat Pandas dataframe columns
  • Python getting common name from URL using ssl.getpeercert()
  • how to get username into sql trigger when multiple users signed on from asp membership
  • pyodbc doesn't report sql server error
  • does jqgrid support a multiple checkbox list for editing
  • Email format validation in mvc3 view
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • How to add date and time under each post in guestbook in google app engine
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • coudnt use logback because of log4j
  • How to stop GridView from loading again when I press back button?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF