public void SaveFor(int ssn, PassportEntity passport)
        {
            using (var session = _factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    string hql = string.Format("INSERT INTO {0} ({1}, {2}, {3}) " +
                                                "SELECT personWithSsn, :passportNumber, :passportIssued FROM {4} personWithSsn WHERE {5} = :ssn",
                        typeof(PassportEntity).Name,
                        MemberName.Of(() => passport.Person),
                        MemberName.Of(() => passport.Number),
                        MemberName.Of(() => passport.Issued),
                        typeof(PersonEntity).Name,
                        MemberName.Of((PersonEntity person) => person.SSN));

                    session.CreateQuery(hql)
                        .SetInt32("passportNumber", passport.Number)
                        .SetDateTime("passportIssued", passport.Issued)
                        .SetInt32("ssn", ssn)
                        .ExecuteUpdate();

                    transaction.Commit();
                }
            }
        }
 public void Save(PassportEntity passport)
 {
     using (var session = _factory.OpenSession())
     {
         using (var transaction = session.BeginTransaction())
         {
             session.SaveOrUpdate(passport);
             transaction.Commit();
         }
     }
 }
        public void SaveLoadWithoutRelations()
        {
            /* Arrange */
            var john = ProducePerson();

            var passport = new PassportEntity {Person = john, Number = 98765, Issued = DateTime.Now};

            /* Act */
            _passportsStorage.Save(passport);
            var loadedPassport = _passportsStorage.LoadAll().FirstOrDefault();

            /* Assert */
            loadedPassport.Should().NotBeNull();

            loadedPassport.Should().NotBeSameAs(passport);
            loadedPassport.Number.Should().Be(passport.Number);
            loadedPassport.Issued.Should().BeWithin(1.Days()).Before(passport.Issued);

            NHibernateUtil.IsInitialized(loadedPassport.Person).Should().BeFalse();
        }
        public void SaveForSpecifiedPersonLoadWithRelations()
        {
            /* Arrange */
            var john = ProducePerson();

            var passport = new PassportEntity { Number = 98765, Issued = DateTime.Now };

            /* Act */
            _passportsStorage.SaveFor(john.SSN, passport);
            var loadedPassport = _passportsStorage.LoadAllWithRelations().FirstOrDefault();

            /* Assert */
            loadedPassport.Should().NotBeNull();

            loadedPassport.Should().NotBeSameAs(passport);
            loadedPassport.Number.Should().Be(passport.Number);

            var loadedOwner = loadedPassport.Person;
            loadedOwner.Should().NotBeNull();
            loadedOwner.Should().NotBeSameAs(john);

            loadedOwner.Name.Should().Be(john.Name);
        }