コード例 #1
0
		public Employment(Employee e, String org)
		{
			PersonName = e.PersonName;
			OrganizationName = org;
			StartDate = DateTime.Today.AddDays(-1);
			e.Employments.Add(this);
		}
コード例 #2
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();
		}