NHibernate IStatelessSession Load Method Equivalent

I am using NHibernate to insert rows into my database. Due to the amount of data I am inserting, I am using IStatelessSession

instead ISession

. The objects I am inserting use the assigned ids (i.e. no hilo or guids are generated - unique ids are assigned to the objects).

My problem is that I have an object (say Foo

) that has a many-to-one reference to another object (say Bar

). I insert all objects first Bar

and this is not a problem.

The problem arises when I want to insert objects Foo

. I know the unique id of each object Bar

, but I don't want to fetch each object Bar

from the database to set a property on the object Foo

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 object Foo

with Id

from (say) 1234 that refers to an object Bar

that has an ID of (say) 4567. I know there is already one Bar

with that ID because I added all the objects earlier Bar

.

How do I add an object Foo

without re-fetching the object Bar

from the database?

+3


source to share


4 answers


It's strange that sometimes, if you take the time to formulate your question, you immediately understand the answer.

What you do is create a mock object with an ID and nothing set.

STEP 1: Insert the panel 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 Foo object with 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, it doesn't require creating a lot of mock objects), I'd appreciate the advice.

+4


source


Save the Bar objects in Dictionary<int, Bar>

after you insert them and assign a reference to the correct Bar object:

var foo = new Foo();
foo.Bar = bars[1234];
session.Save(foo); // there is no session.Insert method

      



You are also working with a solution, but public setting Bar.Id requires a public parameter.

+2


source


You can use session.Get (id), if the session has entities, it will return a proxy and you will create a Foo object by the proxy link without any call to the database.

0


source


This means it does NOT make a trip to the database and is a way to populate the foreign key without having to load the object.

var foo = new Foo
{
  Id = 4567,
  Bar = new Session.Get<Bar>(1234)
};

      

Strike>

Ignore.

0


source







All Articles