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 at 
> http://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