コード例 #1
0
        public void Lazy()
        {
            ISession     s = OpenSession();
            ITransaction t = s.BeginTransaction();
            var          p = new Person {
                Name = "Gavin"
            };
            var p2 = new Person {
                Name = "Emmanuel"
            };
            var e = new Employee(p);

            new Employment(e, "JBoss");
            var old = new Employment(e, "IFA")
            {
                EndDate = DateTime.Today
            };

            s.Persist(p);
            s.Persist(p2);
            t.Commit();
            s.Close();

            s = OpenSession();
            t = s.BeginTransaction();
            p = s.CreateQuery("from Person where name='Gavin'").UniqueResult <Person>();
            Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));

            Assert.That(p.Employee.Person, Is.SameAs(p));
            Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
            Assert.That(p.Employee.Employments.Count, Is.EqualTo(1));

            p2 = s.CreateQuery("from Person where name='Emmanuel'").UniqueResult <Person>();
            Assert.That(p2.Employee, Is.Null);
            Assert.That(p2.Employee, Is.Null);
            t.Commit();
            s.Close();

            s = OpenSession();
            t = s.BeginTransaction();
            p = s.Get <Person>("Gavin");            // The default loader will fetch the employee
            Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Employee"));

            Assert.That(p.Employee.Person, Is.SameAs(p));
            Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
            Assert.That(p.Employee.Employments.Count, Is.EqualTo(1));

            p2 = s.Get <Person>("Emmanuel");
            Assert.That(p2.Employee, Is.Null);
            Assert.That(p2.Employee, Is.Null);
            s.Delete(p2);
            s.Delete(old);
            s.Delete(p);
            t.Commit();
            s.Close();
        }
コード例 #2
0
        public async Task LazyAsync()
        {
            ISession     s = OpenSession();
            ITransaction t = s.BeginTransaction();
            var          p = new Person {
                Name = "Gavin"
            };
            var p2 = new Person {
                Name = "Emmanuel"
            };
            var e = new Employee(p);

            new Employment(e, "JBoss");
            var old = new Employment(e, "IFA")
            {
                EndDate = DateTime.Today
            };

            await(s.PersistAsync(p));
            await(s.PersistAsync(p2));
            await(t.CommitAsync());
            s.Close();

            s = OpenSession();
            t = s.BeginTransaction();
            p = await(s.CreateQuery("from Person where name='Gavin'").UniqueResultAsync <Person>());
            Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));

            Assert.That(p.Employee.Person, Is.SameAs(p));
            Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
            Assert.That(p.Employee.Employments.Count, Is.EqualTo(1));

            p2 = await(s.CreateQuery("from Person where name='Emmanuel'").UniqueResultAsync <Person>());
            Assert.That(p2.Employee, Is.Null);
            Assert.That(p2.Employee, Is.Null);
            await(t.CommitAsync());
            s.Close();

            s = OpenSession();
            t = s.BeginTransaction();
            p = await(s.GetAsync <Person>("Gavin"));
            Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));

            Assert.That(p.Employee.Person, Is.SameAs(p));
            Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
            Assert.That(p.Employee.Employments.Count, Is.EqualTo(1));

            p2 = await(s.GetAsync <Person>("Emmanuel"));
            Assert.That(p2.Employee, Is.Null);
            Assert.That(p2.Employee, Is.Null);
            await(s.DeleteAsync(p2));
            await(s.DeleteAsync(old));
            await(s.DeleteAsync(p));
            await(t.CommitAsync());
            s.Close();
        }
コード例 #3
0
		public virtual bool Equals(Employment other)
		{
			if (ReferenceEquals(null, other))
			{
				return false;
			}
			if (ReferenceEquals(this, other))
			{
				return true;
			}
			return Equals(other.PersonName, PersonName) && Equals(other.OrganizationName, OrganizationName);
		}
コード例 #4
0
ファイル: Employment.cs プロジェクト: jrauber/GH1429
 public virtual bool Equals(Employment other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.PersonName, PersonName) && Equals(other.OrganizationName, OrganizationName));
 }
コード例 #5
0
		public void Lazy()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			var p = new Person {Name = "Gavin"};
			var p2 = new Person {Name = "Emmanuel"};
			var e = new Employee(p);
			new Employment(e, "JBoss");
			var old = new Employment(e, "IFA") {EndDate = DateTime.Today};
			s.Persist(p);
			s.Persist(p2);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateQuery("from Person where name='Gavin'").UniqueResult<Person>();
			Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));

			Assert.That(p.Employee.Person, Is.SameAs(p));
			Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
			Assert.That(p.Employee.Employments.Count, Is.EqualTo(1));

			p2 = s.CreateQuery("from Person where name='Emmanuel'").UniqueResult<Person>();
			Assert.That(p2.Employee, Is.Null);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			p = s.Get<Person>("Gavin");
			Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Employee"));

			Assert.That(p.Employee.Person, Is.SameAs(p));
			Assert.That(NHibernateUtil.IsInitialized(p.Employee.Employments));
			Assert.That(p.Employee.Employments.Count, Is.EqualTo(1));

			p2 = s.Get<Person>("Emmanuel");
			Assert.That(p2.Employee, Is.Null);
			s.Delete(p2);
			s.Delete(old);
			s.Delete(p);
			t.Commit();
			s.Close();
		}