Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 866347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:47:25+00:00 2026-05-15T09:47:25+00:00

How do I save a large collection with NHibernate which has elements that surpass

  • 0

How do I save a large collection with NHibernate which has elements that surpass the amount of memory allowed for the process?

I am trying to save a Video object with nhibernate which has a large number of Screenshots (see below for code). Each Screenshot contains a byte[], so after nhibernate tries to save 10,000 or so records at once, an OutOfMemoryException is thrown. Normally I would try to break up the save and flush the session after every 500 or so records, but in this case, I need to save the collection because it automatically saves the SortOrder and VideoId for me (without the Screenshot having to know that it was a part of a Video). What is the best approach given my situation? Is there a way to break up this save without forcing the Screenshot to have knowledge of its parent Video?

For your reference, here is the code from the simple sample I created:

public class Video
{
    public long Id { get; set; }
    public string Name { get; set; }

    public Video()
    {
        Screenshots = new ArrayList();
    }

    public IList Screenshots { get; set; }
}

public class Screenshot
{
    public long Id { get; set; }
    public byte[] Data { get; set; }
}

And mappings:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="SavingScreenshotsTrial"
                   namespace="SavingScreenshotsTrial"
                   default-access="property">
  <class name="Screenshot"
         lazy="false">
    <id name="Id"
        type="Int64">
      <generator class="hilo"/>
    </id>

    <property name="Data" column="Data" type="BinaryBlob" length="2147483647" not-null="true" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="SavingScreenshotsTrial"
                   namespace="SavingScreenshotsTrial" >
  <class name="Video"
         lazy="false"
         table="Video"
         discriminator-value="0"
         abstract="true">
    <id name="Id"
        type="Int64"
        access="property">
      <generator class="hilo"/>
    </id>
    <property name="Name" />
    <list name="Screenshots"
          cascade="all-delete-orphan"
          lazy="false">
      <key column="VideoId" />
      <index column="SortOrder" />
      <one-to-many class="Screenshot" />
    </list>
  </class>
</hibernate-mapping>

When I try to save a Video with 10000 screenshots, it throws an OutOfMemoryException. Here is the code I’m using:

        using (var session = CreateSession())
        {
            Video video = new Video();
            for (int i = 0; i < 10000; i++)
            {
                video.Screenshots.Add(new Screenshot() {Data = camera.TakeScreenshot(resolution)});
            }
            session.SaveOrUpdate(video);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:47:25+00:00Added an answer on May 15, 2026 at 9:47 am

    For this reason, we have typically had the child entity reference the parent rather than vice versa.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Save that I have a (large) collection of instances of Dictionary. The key value
Suppose my program creates a large array of data which I then save with
I'm trying to do a cascading save on a large object graph using JPA.
I'm trying to read in a large (700GB) file and incrementally process it, but
I Have website I need to save a large amount of pages from. The
my Problem is that I have an asp text box which has a value
I'm trying to insert a large collection of objects into a table using fluent
I have a ListView that contains a large collection of rows with textboxes that
I have a large amount of data stored in a Collection . I would
I have a large amount of Word documents that I need to parse. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.