static void Main(string[] args)
        {
            var sessionFact = CreateSessionFactory(true);

            using (ISession sess = sessionFact.OpenSession())
            {
               using(ITransaction transaction = sess.BeginTransaction())
               {
                    var bruno = new Person { FirstName = "Frank" };
                    sess.Save(bruno);
                    var dani = new Person { FirstName = "Danielle" };
                    sess.Save(dani);
                    Action<string,Person> AddCat = ( catname,  owner) =>
                        {
                            var newcat = new Cat { Name = catname };
                            sess.Save(newcat);
                            owner.AddCat(newcat);
                        };

                    AddCat("Lucky", bruno);
                    AddCat("Lulu", bruno);

                    AddCat("Furball", dani);
                    AddCat("Nibbler", dani);

                    transaction.Commit();
                    sess.Close();
               }
            }

            // list people and their cats
            // this way will lazy load the cats for each person as it gets to each
            // person. Not very efficient as it makes multiple round trips to the cats table(one for each person)
            Console.WriteLine("People and their cats, lazy loading cats:");
            using (ISession sess = sessionFact.OpenSession())
            {
                var people = sess.CreateCriteria<Person>();

                foreach (Person p in people.List<Person>())
                {
                    Console.WriteLine(String.Format("{0} has cats:", p.FirstName));

                    foreach (Cat c in p.Cats)
                    {
                        Console.WriteLine("Cat: {0}", c.Name);
                    }

                }

            }

            // to get all the cats at the same time as querying the people:
            Console.WriteLine("\nPre-fetching each persons cats:");
            using (ISession sess = sessionFact.OpenSession())
            {
                var people = sess.CreateCriteria<Person>()
                            .SetFetchMode("Cats", FetchMode.Eager);

                foreach (Person p in people.List<Person>())
                {
                    Console.WriteLine(String.Format("{0} has cats:", p.FirstName));

                    foreach (Cat c in p.Cats)
                    {
                        Console.WriteLine("Cat: {0}", c.Name);
                    }

                }

            }

            // or using query over:
            Console.WriteLine("\nUsing query over:");
            using (ISession sess = sessionFact.OpenSession())
            {

                IList<Person> people = sess.QueryOver<Person>()
                                            .Where(p => p.FirstName == "Frank" || p.FirstName == "Danielle")
                                            .Left.JoinQueryOver<Cat>(p => p.Cats)
                                            .List();
                foreach (Person p in people)
                {
                    Console.WriteLine(String.Format("{0} has cats:", p.FirstName));

                    foreach (Cat c in p.Cats)
                    {
                        Console.WriteLine("Cat: {0}", c.Name);
                    }

                }
            }
        }
Example #2
0
 public virtual void AddCat(Cat c)
 {
     this.Cats.Add(c);
     c.Person = this;
 }