public virtual void AddOffspring(Animal offSpring) { if (Offspring == null) { Offspring = new HashedSet<Animal>(); } Offspring.Add(offSpring); }
public void JustForFun() { Configuration conf = ConfigureNHibernate(); conf.AddDeserializedMapping(GetMapping(), "Animals_Domain"); SchemaMetadataUpdater.QuoteTableAndColumns(conf); new SchemaExport(conf).Create(false, true); ISessionFactory factory = conf.BuildSessionFactory(); using (ISession s = factory.OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { var polliwog = new Animal { BodyWeight = 12, Description = "Polliwog" }; var catepillar = new Animal { BodyWeight = 10, Description = "Catepillar" }; var frog = new Animal { BodyWeight = 34, Description = "Frog" }; polliwog.Father = frog; frog.AddOffspring(polliwog); var butterfly = new Animal { BodyWeight = 9, Description = "Butterfly" }; catepillar.Mother = butterfly; butterfly.AddOffspring(catepillar); s.Save(frog); s.Save(polliwog); s.Save(butterfly); s.Save(catepillar); var dog = new Dog { BodyWeight = 200, Description = "dog" }; s.Save(dog); var cat = new Cat { BodyWeight = 100, Description = "cat" }; s.Save(cat); var zoo = new Zoo { Name = "Zoo" }; var add = new Address { City = "MEL", Country = "AU", Street = "Main st", PostalCode = "3000" }; zoo.Address = add; Zoo pettingZoo = new PettingZoo { Name = "Petting Zoo" }; var addr = new Address { City = "Sydney", Country = "AU", Street = "High st", PostalCode = "2000" }; pettingZoo.Address = addr; s.Save(zoo); s.Save(pettingZoo); tx.Commit(); } } using (ISession s = factory.OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { s.CreateQuery("delete from Animal where mother is not null or father is not null").ExecuteUpdate(); s.CreateQuery("delete from Animal").ExecuteUpdate(); s.CreateQuery("delete from Zoo").ExecuteUpdate(); tx.Commit(); } } new SchemaExport(conf).Drop(false, true); }