Oskar,

Thanks for the reply on the original question and for the extra
advice.  That's very cool that it automatically checks the records
you've created previously and uses them.

In the cat and toy example, let's say from my front end, I only have
the cat's ID.  Would I be able to add a new toy without loading a copy
of the owner cat from the database?  As in, what would the body of
AddNewToy(Int32 catOwnerId, Toy toyToCreate) look like?  If I leave
the OwnerCatId as a mapped property, NHibernate gives me an error,
"nhibernate Invalid index 2 for this SqlParameterCollection with
Count=2.".  I figured out what the error was based on
http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibernate-and-invalid-index-n-for-this-sqlparametercollection-with-count-n-error.aspx.

Is creating a new instance of the parent object with nothing but the
ID the correct way to go for the scenario where I have only the ID?
As in,

Toy toy1 = new Toy
{
    OwnerCat = new Cat { Id = cat1CreatedViaSql.Id }
  , Shape = "Mouse"
};

Thanks again,

    Don

On Mar 5, 3:57 am, Oskar Berggren <[email protected]> wrote:
> first-level cache
>
> The query, when run in the same session (and not calling Evict()),
> will return the very same cat instances as you have created manually.
> So if you neglect to initialize the collection property in the Cat
> constructor, it will still be null. And if you create the collection,
> it will still not magically contain the toy - nhibernate doesn't know
> about that until the object is recreated from the database.
>
> Also, it seems that Toy.OwnerCatId should propably be Toy.OwnerCat,
> typed as Cat. It is not uncommon to have e.g. Cat.AddToy(Toy t), which
> will both update Cat.Toys collection, and set Toy.OwnerCat. Then use
> cascading.
>
> /Oskar
>
> 2011/3/4 mysterd429 <[email protected]>:
>
> > I have a little test application, and it seems that I need to close a
> > session and reopen it to get lazy loading to work if I've committed
> > changes using that transaction.  What's the reason for that?  If I run
> > this code, I get that dbCat.Toys is null if I define FAIL.  I'd like
> > to know what the rule is so that I don't accidentally run into it in a
> > real application.
>
> > Thanks!
>
> > Test code:
>
> > Utilities.RecreateSchema();
>
> > ISession session = NHibernateHelper.GetCurrentSession();
> > ITransaction tx = session.BeginTransaction();
>
> > Cat mitzi = new Cat
> > {
> >    Name = "Mitzi"
> >  , Sex = 'F'
> >  , Weight = 4.2f
> > };
> > Cat todd = new Cat
> > {
> >    Name = "Todd"
> >  , Sex = 'M'
> >  , Weight = 8f
> > };
>
> > session.Save(mitzi);
> > session.Save(todd);
>
> > Toy mouse = new Toy
> > {
> >    OwnerCatId = mitzi.Id.Value
> >  , Shape = "Mouse"
> > };
>
> > Toy bird = new Toy
> > {
> >    OwnerCatId = todd.Id.Value
> >  , Shape = "Bird"
> > };
>
> > Toy laser = new Toy
> > {
> >    OwnerCatId = todd.Id.Value
> >  , Shape = "Laser"
> > };
>
> > session.Save(mouse);
> > session.Save(bird);
> > session.Save(laser);
>
> > tx.Commit();
>
> > #if !FAIL
> > NHibernateHelper.CloseSession();
> > session = NHibernateHelper.GetCurrentSession();
> > tx = session.BeginTransaction();
> > #endif
>
> > IList<Cat> dbCats = session.Query<Cat>().Where(justACatVariable =>
> > justACatVariable.Toys.Any(t => t.Shape == "Bird")).ToList();
>
> > foreach (Cat dbCat in dbCats)
> > {
> >    Console.Out.WriteLine(
> >        "{0}: {1}"
> >      , dbCat.Id
> >      , dbCat.Name
> >    );
>
> >    foreach (Toy dbToy in dbCat.Toys)
> >    {
> >        Console.Out.WriteLine(
> >            "  {0}: {1}"
> >          , dbToy.ToyId
> >          , dbToy.Shape
> >        );
> >    }
> > }
>
> > List<Toy> dbToys = session.Query<Toy>().ToList();
> > foreach (Toy dbToy in dbToys)
> > {
> >    Console.Out.WriteLine(
> >        "{0}: {1}"
> >      , dbToy.ToyId
> >      , dbToy.Shape
> >    );
> > }
>
> > NHibernateHelper.CloseSession();
>
> > Hibernate Helper:
>
> > using System;
> > using System.Collections.Generic;
> > using System.Linq;
> > using System.Text;
> > using NHibernate;
> > using NHibernate.Cfg;
>
> > namespace QuickStart
> > {
> >    public sealed class NHibernateHelper
> >    {
> >        private static readonly ISessionFactory s_sessionFactory;
> >        private static ISession m_currentSession;
>
> >        static NHibernateHelper()
> >        {
> >            s_sessionFactory = new
> > Configuration().Configure().BuildSessionFactory();
> >        }
>
> >        public static ISession GetCurrentSession()
> >        {
> >            if (m_currentSession == null)
> >            {
> >                m_currentSession = s_sessionFactory.OpenSession();
> >            }
>
> >            return m_currentSession;
> >        }
>
> >        public static void CloseSession()
> >        {
> >            if (m_currentSession != null)
> >            {
> >                m_currentSession.Close();
> >                m_currentSession = null;
> >            }
> >        }
>
> >        public static void CloseSessionFactory()
> >        {
> >            if (s_sessionFactory != null)
> >            {
> >                s_sessionFactory.Close();
> >            }
> >        }
> >    }
> > }
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/nhusers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to