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 7689443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:10:01+00:00 2026-05-31T20:10:01+00:00

I am using NHibernate to bulk-insert rows into my database. Because of the amount

  • 0

I am using NHibernate to bulk-insert rows into my database. Because of the amount of data I am inserting, I am using IStatelessSession instead of ISession. The objects I am inserting use assigned identities (ie no hilo or guids generated — the unique ids are assigned to the objects).

My problem is that I have an object (say Foo) which has a many-to-one reference to another object (say Bar). I insert all of the Bar objects first and that is no problem.

The problem comes when I want to insert the Foo objects. I know the unique identifier of each Bar object, but I don’t want to have to retrieve each Bar object from the database in order to set the property on the Foo object before inserting it.

Now might be a good time to show a simple example:

public class Foo {
    // Unique identifier (assigned)
    public virtual int Id { get; set; }

    // Many-to-one reference to a Bar object
    public virtual Bar Bar { get; set; }
}

public class Bar {
    // Unique identifier (assigned)
    public virtual int Id { get; set; }
}

Let’s say that I want to create a new Foo object with an Id of (say) 1234 that references a Bar object that has an Id of (say) 4567. I know that there is already a Bar object with this identifier because I have added all the Bar objects previously.

How do I go about adding the Foo object without having to retrieve the Bar object from the database again?

  • 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-31T20:10:03+00:00Added an answer on May 31, 2026 at 8:10 pm

    Strange that sometimes if you take the time to formulate your question, you realise the answer shortly after.

    What you do is create a dummy object that has the Id and nothing else set.

    STEP 1: Insert the Bar Object

    using (var session = SessionFactory.OpenStatelessSession())
    {
        using (var tx = session.BeginTransaction())
        {
            var bar = new Bar
            {
                Id = 1234,
                // and populate all of the other
                // properties that you would put here
            };
            session.Insert(bar);
            tx.Commit();
        }
    }
    

    STEP 2: Insert the Foo Object with a dummy Bar object

    using (var session = SessionFactory.OpenStatelessSession())
    {
        using (var tx = session.BeginTransaction())
        {
            var foo = new Foo
            {
                Id = 4567,
                // dummy Bar object that has an Id and nothing else
                Bar = new Bar {Id = 1234}
            };
            session.Insert(foo);
            tx.Commit();
        }
    }
    

    But if anyone has a better way (for example, that does not require creating lots of dummy objects), I’d be grateful for advice.

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

Sidebar

Related Questions

I insert some data into a sql server 2005 database using NHibernate. Before I
I'm using an NHibernate Stateless Session to load bulk data into the database. As
Using NHibernate I need to insert an entity into a database that has a
I am using nhibernate and mvc3 in asp.net I'm trying to add data into
I query my Database using NHibernate. Now I need to restrict the data being
I'm trying to bulk-update entities using a StatelessSession. Because it's stateless, NHibernate doesn't auto-cascade
I'm using NHibernate on a project and I need to do data auditing. I
I'm using nhibernate as my ORM and Firebird embedded as the database. How would
I am using NHibernate to map a class to a database table. The Part
I'm having trouble getting Hibernate to perform a bulk insert on MySQL. I'm using

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.